From bd188f7056397ffb7247ee54d67483e756c05f1e Mon Sep 17 00:00:00 2001 From: YokiiroBW Date: Thu, 30 Jul 2026 02:30:07 +0800 Subject: [PATCH] feat(p5): gate multimodal ingress and egress sinks --- _conf_schema.json | 20 ++ core/chat_import.py | 12 ++ core/operations.py | 16 +- core/p5c_guard.py | 162 ++++++++++++++++ core/qq_history.py | 4 + core/retrieval.py | 31 +++ core/service.py | 68 ++++++- main.py | 42 +++++ page_api.py | 41 ++++ tests/test_p5c_gates.py | 405 ++++++++++++++++++++++++++++++++++++++++ 10 files changed, 799 insertions(+), 2 deletions(-) create mode 100644 core/p5c_guard.py create mode 100644 tests/test_p5c_gates.py diff --git a/_conf_schema.json b/_conf_schema.json index 7915a97..e6fd5a4 100644 --- a/_conf_schema.json +++ b/_conf_schema.json @@ -823,6 +823,26 @@ } } }, + "p5c_safety": { + "description": "P5-C fail-closed sink gates", + "hint": "Each gate is default-off. This repository has no host-issued principal or opaque capability contract, so enabling any one gate causes only its matching sink to return a controlled denial.", + "type": "object", + "items": { + "enable_p5c_file_ingest_gate": {"description": "Gate historical file ingest", "type": "bool", "default": false}, + "enable_p5c_historical_summary_egress_gate": {"description": "Gate historical-summary provider egress", "type": "bool", "default": false}, + "enable_p5c_platform_history_ingest_gate": {"description": "Gate QQ platform-history ingest", "type": "bool", "default": false}, + "enable_p5c_tool_callback_gate": {"description": "Gate LLM tool callbacks", "type": "bool", "default": false}, + "enable_p5c_tool_parameter_gate": {"description": "Gate LLM tool parameter consumption", "type": "bool", "default": false}, + "enable_p5c_tool_output_dlp": {"description": "Gate LLM tool output serialization", "type": "bool", "default": false}, + "enable_p5c_rerank_egress_gate": {"description": "Gate rerank provider egress", "type": "bool", "default": false}, + "enable_p5c_embedding_egress_gate": {"description": "Gate embedding provider egress", "type": "bool", "default": false}, + "enable_p5c_session_summary_egress_gate": {"description": "Gate session-summary provider egress", "type": "bool", "default": false}, + "enable_p5c_decay_summary_egress_gate": {"description": "Gate decay-summary provider egress", "type": "bool", "default": false}, + "enable_p5c_archive_output_dlp": {"description": "Gate archive output", "type": "bool", "default": false}, + "enable_p5c_page_search_output_dlp": {"description": "Gate page-search output", "type": "bool", "default": false}, + "enable_p5c_portable_export_gate": {"description": "Gate portable export creation", "type": "bool", "default": false} + } + }, "maintenance": { "description": "维护", "hint": "控制数据保留和清理策略。", diff --git a/core/chat_import.py b/core/chat_import.py index 4da478f..1d30585 100644 --- a/core/chat_import.py +++ b/core/chat_import.py @@ -16,6 +16,7 @@ from zoneinfo import ZoneInfo from .astrbot_compat import logger from .models import EntityRef, MemoryRecord, SessionContext, clean_text, json_dumps, stable_fingerprint, utc_now +from .p5c_guard import FILE_INGEST_SINK, HISTORICAL_SUMMARY_EGRESS_SINK, require_p5c_gate LOCAL_TZ = ZoneInfo("Asia/Shanghai") @@ -857,6 +858,9 @@ class HistoricalChatImporter: return manifest def stage_upload(self, *, filename: str, content: bytes, base_year: int = 0) -> dict[str, Any]: + # The host supplies only page data here; it is not a P5-C grant. Gate + # before even decoding the uploaded bytes. + require_p5c_gate(getattr(self.service, "config", None), FILE_INGEST_SINK) if len(content) > self.MAX_UPLOAD_BYTES: raise ValueError("对话文件不能超过 8 MiB") base_year = int(base_year or 0) @@ -1078,6 +1082,9 @@ class HistoricalChatImporter: return suggestions async def start_import(self, payload: dict[str, Any]) -> dict[str, Any]: + # A prior staged upload, payload fields, and upload ID do not become + # a P5-C import grant. Deny before loading staged body or writing. + require_p5c_gate(getattr(self.service, "config", None), FILE_INGEST_SINK) upload_id = clean_text(payload.get("upload_id"), 120) manifest, messages = self._load_upload_messages(upload_id) raw_map = payload.get("speaker_map") if isinstance(payload.get("speaker_map"), dict) else {} @@ -1497,6 +1504,11 @@ class HistoricalChatImporter: prompt: str, task: str, ) -> dict[str, Any]: + # There is no host-issued, request-bound provider grant on historical + # import workers. Deny before resolving a provider or consuming prompt. + require_p5c_gate( + getattr(self.service, "config", None), HISTORICAL_SUMMARY_EGRESS_SINK + ) attempts = await self._provider_attempts(batch) if not attempts: raise RuntimeError("没有可用的历史对话总结 Provider") diff --git a/core/operations.py b/core/operations.py index 9a08692..72c3796 100644 --- a/core/operations.py +++ b/core/operations.py @@ -10,6 +10,7 @@ from typing import Any from .config import ConfigView from .models import EntityRef, MemoryRecord, clean_text, json_loads +from .p5c_guard import PORTABLE_EXPORT_SINK, p5c_gate_reason PORTABLE_FORMAT = "astrbot-memory-jsonl" @@ -301,12 +302,25 @@ async def build_operational_report(service: Any) -> dict[str, Any]: class PortableMemoryArchive: - def __init__(self, store: Any, data_dir: Path): + def __init__(self, store: Any, data_dir: Path, *, config: Any = None): self.store = store self.data_dir = Path(data_dir) self.export_dir = self.data_dir / "exports" + self.config = config async def export(self) -> dict[str, Any]: + # The page route supplies no host-authenticated principal or opaque + # export capability. Deny before any data read or directory/file work. + p5c_reason = p5c_gate_reason(self.config, PORTABLE_EXPORT_SINK) + if p5c_reason: + return { + "ok": False, + "error": p5c_reason, + "path": "", + "counts": {}, + "format": PORTABLE_FORMAT, + "version": PORTABLE_VERSION, + } memories = await self.store.list_memories(limit=1_000_000) identities = await self.store.list_identities(limit=1_000_000) relationships = await self.store.list_relationships(limit=1_000_000) diff --git a/core/p5c_guard.py b/core/p5c_guard.py new file mode 100644 index 0000000..137780d --- /dev/null +++ b/core/p5c_guard.py @@ -0,0 +1,162 @@ +"""P5-C default-off fail-closed guards for confirmed Memory sinks. + +The repository has no host-issued principal or opaque sink-capability +contract yet. Consequently an enabled P5-C gate has exactly one live +outcome: a controlled denial before the protected action. In particular, +page payloads, scope dictionaries, session identifiers, events, and ordinary +Python objects are never interpreted as a grant. +""" + +from __future__ import annotations + +import hashlib +import logging +from dataclasses import dataclass +from typing import Any + + +logger = logging.getLogger("MemoryCompanion.P5C") + +FILE_INGEST_SINK = "file_ingest" +HISTORICAL_SUMMARY_EGRESS_SINK = "historical_summary_egress" +PLATFORM_HISTORY_INGEST_SINK = "platform_history_ingest" +TOOL_CALLBACK_SINK = "tool_callback" +TOOL_PARAMETER_SINK = "tool_parameter" +TOOL_OUTPUT_DLP_SINK = "tool_output_dlp" +RERANK_EGRESS_SINK = "rerank_egress" +EMBEDDING_EGRESS_SINK = "embedding_egress" +SESSION_SUMMARY_EGRESS_SINK = "session_summary_egress" +DECAY_SUMMARY_EGRESS_SINK = "decay_summary_egress" +ARCHIVE_OUTPUT_DLP_SINK = "archive_output_dlp" +PAGE_SEARCH_OUTPUT_DLP_SINK = "page_search_output_dlp" +PORTABLE_EXPORT_SINK = "portable_export" + +P5C_CONFIG_MODULE = "p5c_safety" +P5C_SINK_FLAGS = { + FILE_INGEST_SINK: "enable_p5c_file_ingest_gate", + HISTORICAL_SUMMARY_EGRESS_SINK: "enable_p5c_historical_summary_egress_gate", + PLATFORM_HISTORY_INGEST_SINK: "enable_p5c_platform_history_ingest_gate", + TOOL_CALLBACK_SINK: "enable_p5c_tool_callback_gate", + TOOL_PARAMETER_SINK: "enable_p5c_tool_parameter_gate", + TOOL_OUTPUT_DLP_SINK: "enable_p5c_tool_output_dlp", + RERANK_EGRESS_SINK: "enable_p5c_rerank_egress_gate", + EMBEDDING_EGRESS_SINK: "enable_p5c_embedding_egress_gate", + SESSION_SUMMARY_EGRESS_SINK: "enable_p5c_session_summary_egress_gate", + DECAY_SUMMARY_EGRESS_SINK: "enable_p5c_decay_summary_egress_gate", + ARCHIVE_OUTPUT_DLP_SINK: "enable_p5c_archive_output_dlp", + PAGE_SEARCH_OUTPUT_DLP_SINK: "enable_p5c_page_search_output_dlp", + PORTABLE_EXPORT_SINK: "enable_p5c_portable_export_gate", +} + + +class P5CControlledDeny(ValueError): + """A stable, content-free error suitable for an in-process controlled deny.""" + + def __init__(self, sink: str, reason_code: str) -> None: + self.sink = sink + self.reason_code = reason_code + super().__init__(reason_code) + + +@dataclass(frozen=True) +class P5CGateDecision: + allowed: bool + sink: str + reason_code: str + audit: dict[str, str] + + +def _bool(config: Any, flag: str) -> bool: + """Read the documented flag while retaining compatibility with flat config tests.""" + + getter = getattr(config, "bool", None) + if not callable(getter): + return False + try: + if bool(getter(flag, False)): + return True + return bool(getter(f"{P5C_CONFIG_MODULE}.{flag}", False)) + except Exception: + return False + + +def _audit(sink: str, outcome: str, reason_code: str) -> dict[str, str]: + # This is deliberately derived from a static sink label. Do not pass + # content, request IDs, user IDs, scope/session data, or provider payloads + # into this audit projection. + return { + "sink": sink, + "outcome": outcome, + "reason_code": reason_code, + "ref_hash": hashlib.sha256(f"p5c:{sink}".encode("utf-8")).hexdigest(), + } + + +def check_p5c_gate( + config: Any, + sink: str, + *, + principal: Any = None, + capability: Any = None, +) -> P5CGateDecision: + """Evaluate one confirmed P5-C sink without accepting caller-supplied grants. + + ``principal`` and ``capability`` are accepted only to make the boundary + explicit for future host integration. This repository cannot validate or + issue either object, so they intentionally cannot change an enabled deny. + No representation, serialization, or audit field is derived from them. + """ + + # Keep the values opaque: querying attributes, equality, repr(), or + # serializing them would turn arbitrary caller input into an authorization + # or logging surface. + del principal, capability + flag = P5C_SINK_FLAGS.get(sink) + if not flag: + raise ValueError("p5c_unknown_sink") + if not _bool(config, flag): + return P5CGateDecision(True, sink, "default_off", _audit(sink, "bypass", "default_off")) + + reason_code = "p5c_host_contract_unavailable" + audit = _audit(sink, "denied", reason_code) + logger.warning("[MemoryCompanion][P5-C] gate=%s", audit) + return P5CGateDecision(False, sink, reason_code, audit) + + +def p5c_gate_reason(config: Any, sink: str) -> str: + """Return an empty string for the default-off rollback path, else deny.""" + + decision = check_p5c_gate(config, sink) + return "" if decision.allowed else decision.reason_code + + +def require_p5c_gate(config: Any, sink: str) -> None: + """Raise a stable controlled-deny error when an enabled gate has no grant.""" + + decision = check_p5c_gate(config, sink) + if not decision.allowed: + raise P5CControlledDeny(decision.sink, decision.reason_code) + + +__all__ = [ + "ARCHIVE_OUTPUT_DLP_SINK", + "DECAY_SUMMARY_EGRESS_SINK", + "EMBEDDING_EGRESS_SINK", + "FILE_INGEST_SINK", + "HISTORICAL_SUMMARY_EGRESS_SINK", + "P5CControlledDeny", + "P5C_CONFIG_MODULE", + "P5CGateDecision", + "P5C_SINK_FLAGS", + "PAGE_SEARCH_OUTPUT_DLP_SINK", + "PLATFORM_HISTORY_INGEST_SINK", + "PORTABLE_EXPORT_SINK", + "RERANK_EGRESS_SINK", + "SESSION_SUMMARY_EGRESS_SINK", + "TOOL_CALLBACK_SINK", + "TOOL_OUTPUT_DLP_SINK", + "TOOL_PARAMETER_SINK", + "check_p5c_gate", + "p5c_gate_reason", + "require_p5c_gate", +] diff --git a/core/qq_history.py b/core/qq_history.py index 56fd400..9e802d7 100644 --- a/core/qq_history.py +++ b/core/qq_history.py @@ -10,6 +10,7 @@ from zoneinfo import ZoneInfo from .astrbot_compat import logger from .models import clean_text, stable_fingerprint +from .p5c_guard import PLATFORM_HISTORY_INGEST_SINK, require_p5c_gate LOCAL_TZ = ZoneInfo("Asia/Shanghai") @@ -418,6 +419,9 @@ class QQHistoryReader: start_at: Any, end_at: Any, ) -> dict[str, Any]: + # A selected adapter, page scope, and supplied QQ ID are not host + # principal/capability evidence. Stop before the first adapter action. + require_p5c_gate(getattr(self.service, "config", None), PLATFORM_HISTORY_INGEST_SINK) target_id = clean_text(user_id, 40) if not QQ_ID.fullmatch(target_id): raise ValueError("目标 QQ 必须是 5 到 20 位纯数字") diff --git a/core/retrieval.py b/core/retrieval.py index cfed62a..6c49e25 100644 --- a/core/retrieval.py +++ b/core/retrieval.py @@ -10,6 +10,12 @@ from typing import Any from .identity import session_target_id from .models import MemoryRecord, SearchResult, SessionContext, clean_text +from .p5c_guard import ( + EMBEDDING_EGRESS_SINK, + RERANK_EGRESS_SINK, + p5c_gate_reason, + require_p5c_gate, +) from .person_memory import visibility_reason as person_memory_visibility_reason from .store import MemoryStore from .time_intent import TimeIntent @@ -42,6 +48,7 @@ class RetrievalEngine: knowledge_graph_enabled: bool = True, knowledge_graph_expansion_limit: int = 12, p5_recall_provenance_required: bool = False, + p5c_config: Any = None, usage_recorder: Any | None = None, ): self.store = store @@ -71,6 +78,7 @@ class RetrievalEngine: self.knowledge_graph_enabled = bool(knowledge_graph_enabled) self.knowledge_graph_expansion_limit = max(0, int(knowledge_graph_expansion_limit or 0)) self.p5_recall_provenance_required = bool(p5_recall_provenance_required) + self.p5c_config = p5c_config self.usage_recorder = usage_recorder if callable(usage_recorder) else None self._rank_path_info: dict[str, Any] = {} self.last_path_info: dict[str, Any] = { @@ -270,6 +278,19 @@ class RetrievalEngine: } return ranked + p5c_reason = p5c_gate_reason(self.p5c_config, RERANK_EGRESS_SINK) + if p5c_reason: + path = "fallback_basic" if self.retrieval_mode == "rerank" else "basic" + self.last_path_info = { + "mode": self.retrieval_mode, + "path": path, + "provider_id": self.rerank_provider_id, + "reason": p5c_reason, + "candidate_count": len(ranked), + **self._rank_path_info, + } + return ranked + query_text = clean_text(query, 1000) if not query_text: path = "fallback_basic" if self.retrieval_mode == "rerank" else "basic" @@ -406,6 +427,9 @@ class RetrievalEngine: return hits >= max(2, int(profile.get("min_hits", 1) or 1)) async def _call_rerank_provider(self, query: str, documents: list[str]) -> Any: + # Defense in depth for direct callers: do not normalize/query the body + # before the enabled egress gate rejects it. + require_p5c_gate(self.p5c_config, RERANK_EGRESS_SINK) method = self.rerank_provider.rerank query_text = clean_text(query, 1000) document_texts = [clean_text(document, 1200) for document in documents] @@ -1044,6 +1068,10 @@ class RetrievalEngine: } if not self.embedding_enabled: return [], {}, info + p5c_reason = p5c_gate_reason(self.p5c_config, EMBEDDING_EGRESS_SINK) + if p5c_reason: + info["embedding_reason"] = p5c_reason + return [], {}, info if self.embedding_provider is None or not self._is_embedding_provider(self.embedding_provider): info["embedding_reason"] = "no_embedding_provider" return [], {}, info @@ -1114,6 +1142,9 @@ class RetrievalEngine: ) async def _call_embedding_provider(self, text: str) -> list[float]: + # Defense in depth for direct callers; the input is not read or + # normalized until the enabled sink gate has made its deny decision. + require_p5c_gate(self.p5c_config, EMBEDDING_EGRESS_SINK) provider = self.embedding_provider text = clean_text(text, 2000) if provider is None: diff --git a/core/service.py b/core/service.py index 3e61adf..ab260a8 100644 --- a/core/service.py +++ b/core/service.py @@ -95,6 +95,15 @@ from .unified_person_persistence import UnifiedPersonPersistence, UnifiedPersonR from .person_memory import PersonMemoryFacts, PersonMemoryMigration from .p5_provenance_persistence import P5ProvenanceMigration from .p5_b1_recall_gate import evaluate_bridge_serialization_gate, evaluate_normal_recall_gate +from .p5c_guard import ( + ARCHIVE_OUTPUT_DLP_SINK, + DECAY_SUMMARY_EGRESS_SINK, + EMBEDDING_EGRESS_SINK, + SESSION_SUMMARY_EGRESS_SINK, + TOOL_CALLBACK_SINK, + p5c_gate_reason, + require_p5c_gate, +) from .visibility import VisibilityPolicy @@ -212,7 +221,7 @@ class MemoryCompanionService: self.person_memory_migration = PersonMemoryMigration(self.store) self.p5_provenance_migration = P5ProvenanceMigration(self.store) self.person_memory_facts = PersonMemoryFacts(self.store) - self.portable_archive = PortableMemoryArchive(self.store, self.data_dir) + self.portable_archive = PortableMemoryArchive(self.store, self.data_dir, config=self.config) self.chat_importer = HistoricalChatImporter(self) self.qq_history_reader = QQHistoryReader(self) self.sleep_state_path = self.data_dir / "memory_companion_sleep_state.json" @@ -1547,6 +1556,9 @@ class MemoryCompanionService: return payload async def bridge_get_archive_scope(self, *, scope: dict[str, Any] | None = None) -> dict[str, Any]: + p5c_reason = p5c_gate_reason(getattr(self, "config", None), ARCHIVE_OUTPUT_DLP_SINK) + if p5c_reason: + return {"archive_api_version": ARCHIVE_API_VERSION, "warnings": [p5c_reason], "error": p5c_reason} ctx = self._archive_context(scope) return { "archive_api_version": "1.0", @@ -1569,6 +1581,15 @@ class MemoryCompanionService: time_range: dict[str, str] | None = None, include_timeline: bool = True, ) -> dict[str, Any]: + p5c_reason = p5c_gate_reason(getattr(self, "config", None), ARCHIVE_OUTPUT_DLP_SINK) + if p5c_reason: + return { + "archive_api_version": ARCHIVE_API_VERSION, + "memories": [], + "excluded": [], + "warnings": [p5c_reason], + "error": p5c_reason, + } p5_b2_reason = self._p5_b2_archive_read_gate_reason() if p5_b2_reason: return { @@ -1648,6 +1669,15 @@ class MemoryCompanionService: scope: dict[str, Any], include_timeline: bool = True, ) -> dict[str, Any]: + p5c_reason = p5c_gate_reason(getattr(self, "config", None), ARCHIVE_OUTPUT_DLP_SINK) + if p5c_reason: + return { + "archive_api_version": ARCHIVE_API_VERSION, + "memories": [], + "excluded": [], + "warnings": [p5c_reason], + "error": p5c_reason, + } p5_b2_reason = self._p5_b2_archive_read_gate_reason() if p5_b2_reason: return { @@ -1768,6 +1798,14 @@ class MemoryCompanionService: limit: int = 30, include_timeline: bool = True, ) -> dict[str, Any]: + p5c_reason = p5c_gate_reason(getattr(self, "config", None), ARCHIVE_OUTPUT_DLP_SINK) + if p5c_reason: + return { + "archive_api_version": ARCHIVE_API_VERSION, + "items": [], + "warnings": [p5c_reason], + "error": p5c_reason, + } p5_b2_reason = self._p5_b2_archive_read_gate_reason() if p5_b2_reason: return { @@ -2612,6 +2650,7 @@ class MemoryCompanionService: embedding_enabled=False, knowledge_graph_enabled=False, p5_recall_provenance_required=self.config.bool("enable_p5_b1_recall_gate", False), + p5c_config=self.config, ) async def _retrieval_engine(self, ctx: SessionContext, *, admin_read_all: bool = False) -> RetrievalEngine: @@ -2654,6 +2693,7 @@ class MemoryCompanionService: knowledge_graph_enabled=self.config.bool("knowledge_graph.retrieval_expansion_enabled", True), knowledge_graph_expansion_limit=self.config.int("knowledge_graph.expansion_limit", 12), p5_recall_provenance_required=self.config.bool("enable_p5_b1_recall_gate", False), + p5c_config=self.config, usage_recorder=self._record_token_usage, ) @@ -3161,6 +3201,10 @@ class MemoryCompanionService: self._embedding_backfill_inflight.discard(provider_id) async def _embed_memory_record(self, provider: Any, provider_id: str, record: MemoryRecord) -> bool: + # P5-B provenance eligibility remains a necessary condition below; + # it is never an egress grant for P5-C. + if p5c_gate_reason(getattr(self, "config", None), EMBEDDING_EGRESS_SINK): + return False if record.lifecycle == "archived": return False if record.review_status == "pending" and not self.config.bool("retrieval.embedding_index_pending", False): @@ -3206,6 +3250,7 @@ class MemoryCompanionService: return False async def _embed_text_with_provider(self, provider: Any, text: str, *, provider_id: str = "") -> list[float]: + require_p5c_gate(self.config, EMBEDDING_EGRESS_SINK) text = clean_text(text, max(200, self.config.int("retrieval.embedding_max_text_chars", 1200))) async def wait_result(value: Any) -> Any: @@ -3568,6 +3613,11 @@ class MemoryCompanionService: if age_seconds < retry_delay: return "" + p5c_reason = p5c_gate_reason(getattr(self, "config", None), SESSION_SUMMARY_EGRESS_SINK) + if p5c_reason: + # There is no host-issued sink grant on this background path; + # keep the existing timeline/failure state untouched. + return "" summary_attempts = await self._summary_provider_attempts(ctx) if not summary_attempts: logger.warning("[MemoryCompanion] 无可用 Provider,跳过阶段性记忆总结: session=%s", ctx.session_id) @@ -4188,6 +4238,9 @@ class MemoryCompanionService: return memory_id async def tool_remember(self, event: Any, content: str, *, note_type: str = "memory") -> dict[str, Any]: + p5c_reason = p5c_gate_reason(getattr(self, "config", None), TOOL_CALLBACK_SINK) + if p5c_reason: + return {"ok": False, "error": p5c_reason} ctx = await self.identity.resolve_event_context(event) state = self._p3_prepare_context(ctx, event=event) reason = self._p3_gate_reason(ctx, event=event, state=state) @@ -4227,6 +4280,9 @@ class MemoryCompanionService: return {"ok": True, "memory_id": memory_id, "review_status": "auto"} async def tool_recall(self, event: Any, query: str, top_k: int = 5) -> dict[str, Any]: + p5c_reason = p5c_gate_reason(getattr(self, "config", None), TOOL_CALLBACK_SINK) + if p5c_reason: + return {"ok": False, "error": p5c_reason, "memories": []} ctx = await self.identity.resolve_event_context(event) state = self._p3_prepare_context(ctx, event=event) reason = self._p3_gate_reason(ctx, event=event, state=state) @@ -4245,6 +4301,9 @@ class MemoryCompanionService: } async def tool_note_create(self, event: Any, title: str, content: str = "") -> dict[str, Any]: + p5c_reason = p5c_gate_reason(getattr(self, "config", None), TOOL_CALLBACK_SINK) + if p5c_reason: + return {"ok": False, "error": p5c_reason} return { "ok": False, "error": "bot_self_notes_disabled:user_memory_contract", @@ -4253,6 +4312,9 @@ class MemoryCompanionService: } async def tool_note_read(self, event: Any, query: str = "", limit: int = 5) -> dict[str, Any]: + p5c_reason = p5c_gate_reason(getattr(self, "config", None), TOOL_CALLBACK_SINK) + if p5c_reason: + return {"ok": False, "error": p5c_reason, "notes": []} ctx = await self.identity.resolve_event_context(event) query = clean_text(query, 500) records = await self.store.list_memories( @@ -4720,6 +4782,9 @@ class MemoryCompanionService: records = [item["record"] for item in items] sample = records[0] ctx = self._decay_context(sample) + p5c_reason = p5c_gate_reason(getattr(self, "config", None), DECAY_SUMMARY_EGRESS_SINK) + if p5c_reason: + return {"bucket": group.get("bucket"), "summary_id": "", "archived": 0, "reason": p5c_reason} attempts = await self._summary_provider_attempts(ctx) if not attempts: return {"bucket": group.get("bucket"), "summary_id": "", "archived": 0, "reason": "no_summary_provider"} @@ -4903,6 +4968,7 @@ class MemoryCompanionService: ctx: SessionContext, items: list[dict[str, Any]], ) -> str: + require_p5c_gate(self.config, DECAY_SUMMARY_EGRESS_SINK) lines: list[str] = [] total = 0 max_input_chars = self.config.int("maintenance.memory_decay_summary_input_chars", 6000) diff --git a/main.py b/main.py index 66177e5..efc94df 100644 --- a/main.py +++ b/main.py @@ -14,6 +14,12 @@ from .core.bridge import MemoryCompanionBridge from .core.context_orchestration import MEMORY_CONTEXT as HOOK_PRIORITY_MEMORY_CONTEXT from .core.commands import MemoryCompanionCommandHandler from .core.models import json_dumps +from .core.p5c_guard import ( + TOOL_CALLBACK_SINK, + TOOL_OUTPUT_DLP_SINK, + TOOL_PARAMETER_SINK, + p5c_gate_reason, +) from .core.service import MemoryCompanionService from .core.runtime_compat import probe_runtime_capabilities from .plugin_identity import PLUGIN_DISPLAY_NAME, PLUGIN_NAME, PLUGIN_REPO, PLUGIN_VERSION @@ -106,6 +112,12 @@ class MemoryCompanionPlugin(Star): query(string): 要回忆的关键词或自然语言问题。 top_k(number): 最多返回几条,默认 5,最多 10。 """ + p5c_reason = p5c_gate_reason(self.service.config, TOOL_CALLBACK_SINK) + if p5c_reason: + return json_dumps({"ok": False, "error": p5c_reason}) + p5c_reason = p5c_gate_reason(self.service.config, TOOL_PARAMETER_SINK) + if p5c_reason: + return json_dumps({"ok": False, "error": p5c_reason}) if not self.service.config.bool("memory_tools.enable_recall_tool", True): return json_dumps({"ok": False, "error": "recall tool disabled"}) result = await self.service.tool_recall( @@ -113,6 +125,9 @@ class MemoryCompanionPlugin(Star): str(kwargs.get("query") or ""), int(kwargs.get("top_k") or 5), ) + p5c_reason = p5c_gate_reason(self.service.config, TOOL_OUTPUT_DLP_SINK) + if p5c_reason: + return json_dumps({"ok": False, "error": p5c_reason}) return json_dumps(result) @filter.llm_tool(name="memory_companion_remember") @@ -125,6 +140,12 @@ class MemoryCompanionPlugin(Star): content(string): 要保存的记忆内容。 note_type(string): memory/preference/relationship/promise 等简短类别。 """ + p5c_reason = p5c_gate_reason(self.service.config, TOOL_CALLBACK_SINK) + if p5c_reason: + return json_dumps({"ok": False, "error": p5c_reason}) + p5c_reason = p5c_gate_reason(self.service.config, TOOL_PARAMETER_SINK) + if p5c_reason: + return json_dumps({"ok": False, "error": p5c_reason}) if not self.service.config.bool("memory_tools.enable_remember_tool", True): return json_dumps({"ok": False, "error": "remember tool disabled"}) result = await self.service.tool_remember( @@ -132,6 +153,9 @@ class MemoryCompanionPlugin(Star): str(kwargs.get("content") or ""), note_type=str(kwargs.get("note_type") or "memory"), ) + p5c_reason = p5c_gate_reason(self.service.config, TOOL_OUTPUT_DLP_SINK) + if p5c_reason: + return json_dumps({"ok": False, "error": p5c_reason}) return json_dumps(result) @filter.llm_tool(name="memory_companion_note_create") @@ -142,6 +166,12 @@ class MemoryCompanionPlugin(Star): title(string): 笔记标题或分类。 content(string): 笔记正文。 """ + p5c_reason = p5c_gate_reason(self.service.config, TOOL_CALLBACK_SINK) + if p5c_reason: + return json_dumps({"ok": False, "error": p5c_reason}) + p5c_reason = p5c_gate_reason(self.service.config, TOOL_PARAMETER_SINK) + if p5c_reason: + return json_dumps({"ok": False, "error": p5c_reason}) if not self.service.config.bool("memory_tools.enable_note_tools", True): return json_dumps({"ok": False, "error": "note tools disabled"}) result = await self.service.tool_note_create( @@ -149,6 +179,9 @@ class MemoryCompanionPlugin(Star): str(kwargs.get("title") or ""), str(kwargs.get("content") or ""), ) + p5c_reason = p5c_gate_reason(self.service.config, TOOL_OUTPUT_DLP_SINK) + if p5c_reason: + return json_dumps({"ok": False, "error": p5c_reason}) return json_dumps(result) @filter.llm_tool(name="memory_companion_note_read") @@ -159,6 +192,12 @@ class MemoryCompanionPlugin(Star): query(string): 可选关键词。 limit(number): 最多读取几条,默认 5,最多 20。 """ + p5c_reason = p5c_gate_reason(self.service.config, TOOL_CALLBACK_SINK) + if p5c_reason: + return json_dumps({"ok": False, "error": p5c_reason}) + p5c_reason = p5c_gate_reason(self.service.config, TOOL_PARAMETER_SINK) + if p5c_reason: + return json_dumps({"ok": False, "error": p5c_reason}) if not self.service.config.bool("memory_tools.enable_note_tools", True): return json_dumps({"ok": False, "error": "note tools disabled"}) result = await self.service.tool_note_read( @@ -166,6 +205,9 @@ class MemoryCompanionPlugin(Star): str(kwargs.get("query") or ""), int(kwargs.get("limit") or 5), ) + p5c_reason = p5c_gate_reason(self.service.config, TOOL_OUTPUT_DLP_SINK) + if p5c_reason: + return json_dumps({"ok": False, "error": p5c_reason}) return json_dumps(result) @filter.command_group("mcomp") diff --git a/page_api.py b/page_api.py index f867efc..505d7ec 100644 --- a/page_api.py +++ b/page_api.py @@ -22,12 +22,26 @@ try: from .core.bridge import serialize_memory from .core.identity import normalize_session_context_fields from .core.models import SessionContext, clean_text + from .core.p5c_guard import ( + ARCHIVE_OUTPUT_DLP_SINK, + FILE_INGEST_SINK, + PAGE_SEARCH_OUTPUT_DLP_SINK, + PORTABLE_EXPORT_SINK, + p5c_gate_reason, + ) from .core.user_memory_boundary import USER_MEMORY_DOMAIN except ImportError: from core.bot_profiles import describe_bot_profiles from core.bridge import serialize_memory from core.identity import normalize_session_context_fields from core.models import SessionContext, clean_text + from core.p5c_guard import ( + ARCHIVE_OUTPUT_DLP_SINK, + FILE_INGEST_SINK, + PAGE_SEARCH_OUTPUT_DLP_SINK, + PORTABLE_EXPORT_SINK, + p5c_gate_reason, + ) from core.user_memory_boundary import USER_MEMORY_DOMAIN try: @@ -63,6 +77,10 @@ class PluginPageApi: def __init__(self, plugin: Any) -> None: self.plugin = plugin + def _p5c_gate_reason(self, sink: str) -> str: + service = getattr(self.plugin, "service", None) + return p5c_gate_reason(getattr(service, "config", None), sink) + def register_routes(self) -> None: register = self.plugin.context.register_web_api routes = [ @@ -152,6 +170,9 @@ class PluginPageApi: return self._err(f"应用预设失败: {exc}", 500) async def data_export(self): + p5c_reason = self._p5c_gate_reason(PORTABLE_EXPORT_SINK) + if p5c_reason: + return self._err(p5c_reason, 403) try: return self._ok({"result": await self.plugin.service.export_portable_data()}) except Exception as exc: @@ -180,6 +201,11 @@ class PluginPageApi: async def conversation_import_upload(self): payload = await self._json() + # The request payload is not a host principal or sink capability. + # Stop before decoding caller-controlled base64 into a file body. + p5c_reason = self._p5c_gate_reason(FILE_INGEST_SINK) + if p5c_reason: + return self._err(p5c_reason, 403) filename = clean_text(payload.get("filename"), 240) or "conversation.txt" encoded = str(payload.get("content_base64") or "").strip() if not encoded: @@ -674,11 +700,17 @@ class PluginPageApi: return self._ok(self.plugin.service.bridge_probe_archive_capabilities()) async def archive_scope(self): + p5c_reason = self._p5c_gate_reason(ARCHIVE_OUTPUT_DLP_SINK) + if p5c_reason: + return self._err(p5c_reason, 403) payload = await self._json() scope = payload.get("scope") if isinstance(payload.get("scope"), dict) else payload return self._ok(await self.plugin.service.bridge_get_archive_scope(scope=scope)) async def archive_search(self): + p5c_reason = self._p5c_gate_reason(ARCHIVE_OUTPUT_DLP_SINK) + if p5c_reason: + return self._err(p5c_reason, 403) payload = await self._json() scope = payload.get("scope") if isinstance(payload.get("scope"), dict) else {} time_range = payload.get("time_range") if isinstance(payload.get("time_range"), dict) else None @@ -692,6 +724,9 @@ class PluginPageApi: return self._ok(result) async def archive_evidence(self): + p5c_reason = self._p5c_gate_reason(ARCHIVE_OUTPUT_DLP_SINK) + if p5c_reason: + return self._err(p5c_reason, 403) payload = await self._json() scope = payload.get("scope") if isinstance(payload.get("scope"), dict) else {} memory_ids = payload.get("memory_ids") if isinstance(payload.get("memory_ids"), list) else [] @@ -703,6 +738,9 @@ class PluginPageApi: return self._ok(result) async def archive_session_context(self): + p5c_reason = self._p5c_gate_reason(ARCHIVE_OUTPUT_DLP_SINK) + if p5c_reason: + return self._err(p5c_reason, 403) payload = await self._json() scope = payload.get("scope") if isinstance(payload.get("scope"), dict) else {} start_at = clean_text(payload.get("start_at"), 80) @@ -719,6 +757,9 @@ class PluginPageApi: return self._ok(result) async def search(self): + p5c_reason = self._p5c_gate_reason(PAGE_SEARCH_OUTPUT_DLP_SINK) + if p5c_reason: + return self._err(p5c_reason, 403) payload = await self._json() query = clean_text(payload.get("query"), 500) if not query: diff --git a/tests/test_p5c_gates.py b/tests/test_p5c_gates.py new file mode 100644 index 0000000..812bb4c --- /dev/null +++ b/tests/test_p5c_gates.py @@ -0,0 +1,405 @@ +from __future__ import annotations + +import asyncio +import sys +import tempfile +import types +import unittest +from pathlib import Path +from types import SimpleNamespace + +from core.chat_import import HistoricalChatImporter +from core.models import EntityRef, MemoryRecord, SearchResult, SessionContext +from core.operations import PortableMemoryArchive +from core.p5c_guard import ( + ARCHIVE_OUTPUT_DLP_SINK, + DECAY_SUMMARY_EGRESS_SINK, + EMBEDDING_EGRESS_SINK, + FILE_INGEST_SINK, + HISTORICAL_SUMMARY_EGRESS_SINK, + P5CControlledDeny, + P5C_SINK_FLAGS, + PAGE_SEARCH_OUTPUT_DLP_SINK, + PLATFORM_HISTORY_INGEST_SINK, + PORTABLE_EXPORT_SINK, + RERANK_EGRESS_SINK, + SESSION_SUMMARY_EGRESS_SINK, + TOOL_CALLBACK_SINK, + check_p5c_gate, +) +from core.qq_history import QQHistoryReader +from core.retrieval import RetrievalEngine +from core.service import MemoryCompanionService +from core.visibility import VisibilityPolicy + +# The focused P5-C page-boundary test only exercises the early guard and does +# not need Quart's request implementation. Keep the existing isolated P5-B +# test environment dependency-free. +try: + import quart # type: ignore # noqa: F401 +except ModuleNotFoundError: + quart_stub = types.ModuleType("quart") + quart_stub.jsonify = lambda payload: payload + quart_stub.request = SimpleNamespace() + quart_stub.send_file = lambda *_args, **_kwargs: None + sys.modules["quart"] = quart_stub + +import page_api as page_api_module +from page_api import PluginPageApi + + +class _Config: + def __init__(self, **flags: bool) -> None: + self.flags = flags + + def bool(self, key: str, default: bool = False) -> bool: + return bool(self.flags.get(key, default)) + + def int(self, _key: str, default: int = 0) -> int: + return default + + def float(self, _key: str, default: float = 0.0) -> float: + return default + + def get(self, _key: str, default=None): + return default + + +class _PoisonedClaim: + def __repr__(self) -> str: # pragma: no cover - only called on a regression + raise AssertionError("a supplied principal/capability must not be inspected or logged") + + +class _RerankProvider: + def __init__(self) -> None: + self.calls = 0 + + async def rerank(self, **_kwargs): + self.calls += 1 + return [] + + +class _EmbeddingProvider: + def __init__(self) -> None: + self.calls = 0 + + async def get_embedding(self, _text: str): + self.calls += 1 + return [1.0, 0.0] + + +class _ForbiddenStore: + def __init__(self) -> None: + self.calls = 0 + + def __getattr__(self, _name: str): + async def forbidden(*_args, **_kwargs): + self.calls += 1 + raise AssertionError("enabled P5-C gate must stop before data access") + + return forbidden + + +class P5CGuardTests(unittest.TestCase): + def test_every_sink_is_default_off_and_fake_claims_never_allow_enabled_gate(self) -> None: + for sink in P5C_SINK_FLAGS: + decision = check_p5c_gate(_Config(), sink) + self.assertTrue(decision.allowed, sink) + self.assertEqual("default_off", decision.reason_code) + + sink = FILE_INGEST_SINK + enabled = _Config(**{P5C_SINK_FLAGS[sink]: True}) + with self.assertLogs("MemoryCompanion.P5C", level="WARNING") as logs: + decision = check_p5c_gate( + enabled, + sink, + principal={"authenticated": True, "user_id": "secret-user"}, + capability=_PoisonedClaim(), + ) + self.assertFalse(decision.allowed) + self.assertEqual("p5c_host_contract_unavailable", decision.reason_code) + self.assertEqual({"sink", "outcome", "reason_code", "ref_hash"}, set(decision.audit)) + self.assertNotIn("secret-user", "\n".join(logs.output)) + self.assertNotIn("PoisonedClaim", "\n".join(logs.output)) + + +class P5CIngressAndProviderTests(unittest.IsolatedAsyncioTestCase): + async def test_file_and_qq_gates_stop_before_decode_or_adapter_action(self) -> None: + with tempfile.TemporaryDirectory() as temp: + config = _Config(enable_p5c_file_ingest_gate=True) + importer = HistoricalChatImporter( + SimpleNamespace(data_dir=Path(temp), store=object(), config=config) + ) + decoded = False + + def decode(_content: bytes): + nonlocal decoded + decoded = True + return "u: 2026-01-01 00:00:00\nsecret", "utf-8" + + importer._decode_upload = decode + with self.assertRaises(P5CControlledDeny): + importer.stage_upload(filename="secret.txt", content=b"secret body") + self.assertFalse(decoded) + + load_calls = 0 + + def forbidden_load(_upload_id: str): + nonlocal load_calls + load_calls += 1 + raise AssertionError("staged upload must not be loaded") + + importer._load_upload_messages = forbidden_load + with self.assertRaises(P5CControlledDeny): + await importer.start_import({"upload_id": "forged-upload"}) + self.assertEqual(0, load_calls) + + page_service = SimpleNamespace(config=_Config(enable_p5c_file_ingest_gate=True)) + api = PluginPageApi(SimpleNamespace(service=page_service)) + api._err = lambda message, status=500: {"error": message, "status": status} + + async def payload_with_base64(): + return {"filename": "secret.txt", "content_base64": "c2VjcmV0"} + + api._json = payload_with_base64 + decoded_bodies = 0 + original_decode = page_api_module.base64.b64decode + + def forbidden_base64_decode(*_args, **_kwargs): + nonlocal decoded_bodies + decoded_bodies += 1 + raise AssertionError("base64 body must not be decoded") + + page_api_module.base64.b64decode = forbidden_base64_decode + try: + response = await api.conversation_import_upload() + finally: + page_api_module.base64.b64decode = original_decode + self.assertEqual({"error": "p5c_host_contract_unavailable", "status": 403}, response) + self.assertEqual(0, decoded_bodies) + + adapter_calls = 0 + + class _Adapter: + async def call_action(self, *_args, **_kwargs): + nonlocal adapter_calls + adapter_calls += 1 + raise AssertionError("adapter action must not run") + + reader = QQHistoryReader( + SimpleNamespace( + config=_Config(enable_p5c_platform_history_ingest_gate=True), + context=SimpleNamespace(platform_manager=SimpleNamespace(get_insts=lambda: [_Adapter()])), + ) + ) + with self.assertRaises(P5CControlledDeny): + await reader.read( + platform_id="forged-platform", + user_id="forged-user", + start_at="not-a-date", + end_at="also-not-a-date", + ) + self.assertEqual(0, adapter_calls) + + async def test_historical_summary_rerank_and_embedding_stop_before_provider(self) -> None: + with tempfile.TemporaryDirectory() as temp: + importer = HistoricalChatImporter( + SimpleNamespace( + data_dir=Path(temp), + store=object(), + config=_Config(enable_p5c_historical_summary_egress_gate=True), + ) + ) + attempts = 0 + + async def forbidden_attempts(_batch): + nonlocal attempts + attempts += 1 + raise AssertionError("provider lookup must not run") + + importer._provider_attempts = forbidden_attempts + with self.assertRaises(P5CControlledDeny): + await importer._call_json_provider({}, prompt="secret prompt", task="historical_chat_summary") + self.assertEqual(0, attempts) + + record = MemoryRecord( + id="m1", + memory_type="manual_memory", + subject=EntityRef(kind="user", id="u1"), + scope="private", + session_id="s1", + content="secret memory body", + ) + reranker = _RerankProvider() + rerank_engine = RetrievalEngine( + object(), + VisibilityPolicy(enable_acl_rules=False), + retrieval_mode="rerank", + rerank_provider=reranker, + p5c_config=_Config(enable_p5c_rerank_egress_gate=True), + ) + ranked = [SearchResult(memory=record, score=1.0)] + self.assertEqual(ranked, await rerank_engine._maybe_rerank_results("secret query", ranked, 1)) + self.assertEqual(0, reranker.calls) + + embedder = _EmbeddingProvider() + embedding_engine = RetrievalEngine( + object(), + VisibilityPolicy(enable_acl_rules=False), + embedding_enabled=True, + embedding_provider=embedder, + embedding_provider_id="embed", + p5c_config=_Config(enable_p5c_embedding_egress_gate=True), + ) + candidates, scores, info = await embedding_engine._embedding_candidate_memories( + "secret query", include_pending=False + ) + self.assertEqual(([], {}), (candidates, scores)) + self.assertEqual("p5c_host_contract_unavailable", info["embedding_reason"]) + self.assertEqual(0, embedder.calls) + + async def test_session_decay_and_background_embedding_do_not_treat_p5b_as_egress_grant(self) -> None: + service = object.__new__(MemoryCompanionService) + service.config = _Config( + enable_p5c_session_summary_egress_gate=True, + enable_p5c_embedding_egress_gate=True, + enable_p5_b1_recall_gate=True, + ) + service._summary_locks = {} + service._summary_lock_ts = {} + service._cleanup_stale_summary_locks = lambda: None + service._p3_gate_reason = lambda *_args, **_kwargs: "" + service.summarizer = SimpleNamespace(interval_elapsed=lambda *_args, **_kwargs: True) + + class _WindowStore: + async def unsummarized_timeline_window(self, **_kwargs): + return { + "rows": [{"id": "row-1", "content": "secret timeline body"}], + "total": 1, + "first_occurred_at": "2026-01-01T00:00:00+00:00", + } + + async def get_summary_failure(self, _session_id): + return None + + service.store = _WindowStore() + summary_provider_calls = 0 + + async def forbidden_summary_provider(_ctx): + nonlocal summary_provider_calls + summary_provider_calls += 1 + raise AssertionError("summary provider lookup must not run") + + service._summary_provider_attempts = forbidden_summary_provider + self.assertEqual("", await service.maybe_summarize_session(SessionContext(session_id="s1", scope="private"), force=True)) + self.assertEqual(0, summary_provider_calls) + + record = MemoryRecord( + id="m2", + memory_type="manual_memory", + subject=EntityRef(kind="user", id="u1"), + scope="private", + session_id="s1", + content="secret embedding body", + ) + provider = _EmbeddingProvider() + self.assertFalse(await service._embed_memory_record(provider, "embed", record)) + self.assertEqual(0, provider.calls) + + service.config = _Config(enable_p5c_decay_summary_egress_gate=True) + with self.assertRaises(P5CControlledDeny): + await service._summarize_decay_records_with_provider( + provider, + ctx=SessionContext(session_id="s1", scope="private"), + items=[{"record": record, "score": 1.0}], + ) + self.assertEqual(0, provider.calls) + + +class P5COutputTests(unittest.IsolatedAsyncioTestCase): + async def test_archive_default_off_keeps_uninitialized_service_compatible(self) -> None: + # P3 compatibility constructs this service without __init__. P5-C + # must treat the absent config as default-off rather than raising. + service = object.__new__(MemoryCompanionService) + service._p5_b2_archive_read_gate_reason = lambda: "p3_unavailable" + + async def archive_scope(*, scope=None): + return {"scope": scope or {}} + + service.bridge_get_archive_scope = archive_scope + response = await service.bridge_search_archive_memories( + "query", scope={"session_id": "private:test"}, limit=1 + ) + self.assertEqual([], response["memories"]) + self.assertEqual(["p3_unavailable"], response["warnings"]) + + async def test_archive_portable_and_page_output_gates_return_safe_controlled_denials(self) -> None: + service = object.__new__(MemoryCompanionService) + service.config = _Config(enable_p5c_archive_output_dlp=True) + service.store = _ForbiddenStore() + + archive = await service.bridge_search_archive_memories( + "secret query", scope={"user_id": "forged-user", "session_id": "forged-session"} + ) + scope = await service.bridge_get_archive_scope(scope={"user_id": "forged-user"}) + evidence = await service.bridge_get_archive_evidence( + ["secret-memory"], scope={"user_id": "forged-user"} + ) + context = await service.bridge_get_archive_session_context( + scope={"user_id": "forged-user"}, start_at="2026-01-01", end_at="2026-01-02" + ) + self.assertEqual([], archive["memories"]) + self.assertNotIn("user_id", scope) + self.assertEqual([], evidence["memories"]) + self.assertEqual([], context["items"]) + self.assertEqual(0, service.store.calls) + + store = _ForbiddenStore() + with tempfile.TemporaryDirectory() as temp: + portable = PortableMemoryArchive( + store, + Path(temp), + config=_Config(enable_p5c_portable_export_gate=True), + ) + export = await portable.export() + self.assertFalse(export["ok"]) + self.assertEqual("", export["path"]) + self.assertFalse((Path(temp) / "exports").exists()) + self.assertEqual(0, store.calls) + + page_service = SimpleNamespace(config=_Config(enable_p5c_page_search_output_dlp=True)) + api = PluginPageApi(SimpleNamespace(service=page_service)) + json_reads = 0 + + async def forbidden_json(): + nonlocal json_reads + json_reads += 1 + raise AssertionError("page JSON must not be parsed for authorization") + + api._json = forbidden_json + api._err = lambda message, status=500: {"error": message, "status": status} + response = await api.search() + self.assertEqual({"error": "p5c_host_contract_unavailable", "status": 403}, response) + self.assertEqual(0, json_reads) + + async def test_tool_callback_gate_stops_before_event_identity_lookup(self) -> None: + service = object.__new__(MemoryCompanionService) + service.config = _Config(enable_p5c_tool_callback_gate=True) + identity_calls = 0 + + class _Identity: + async def resolve_event_context(self, _event): + nonlocal identity_calls + identity_calls += 1 + raise AssertionError("tool callback gate must not inspect event identity") + + service.identity = _Identity() + result = await service.tool_recall(object(), "secret tool parameter") + self.assertEqual( + {"ok": False, "error": "p5c_host_contract_unavailable", "memories": []}, result + ) + self.assertEqual(0, identity_calls) + + +if __name__ == "__main__": + unittest.main()