From 72273e1ffb7bfc7a740282ed9a097dbaf7c3fb92 Mon Sep 17 00:00:00 2001 From: YokiiroBW Date: Wed, 29 Jul 2026 21:33:29 +0800 Subject: [PATCH] feat: add P3 orchestration and P4 boundary coverage --- core/bridge.py | 59 ++- core/context_orchestration.py | 192 ++++++++++ core/models.py | 3 + core/service.py | 373 ++++++++++++++++--- main.py | 3 +- tests/fixtures/context_orchestration_v1.json | 1 + tests/test_p3_context_orchestration.py | 268 +++++++++++++ tests/test_p4_memory_boundary.py | 154 ++++++++ tests/test_summary_relationship.py | 22 +- 9 files changed, 1012 insertions(+), 63 deletions(-) create mode 100644 core/context_orchestration.py create mode 100644 tests/fixtures/context_orchestration_v1.json create mode 100644 tests/test_p3_context_orchestration.py create mode 100644 tests/test_p4_memory_boundary.py diff --git a/core/bridge.py b/core/bridge.py index f0a7728..ec57442 100644 --- a/core/bridge.py +++ b/core/bridge.py @@ -5,6 +5,7 @@ from typing import Any from zoneinfo import ZoneInfo from .models import EntityRef, MemoryRecord, SessionContext, clean_text +from .identity import session_target_id from .archive_contract import user_fact_query_capabilities from .ops_domain import OpsDomainContract, normalize_ops_audit, normalize_ops_event, normalize_ops_task from .temporal_context import build_reply_delay, build_temporal_memory_meta @@ -13,6 +14,7 @@ from .unified_person_contract import ( contract_capabilities, ) from .person_memory_contract import capabilities as person_memory_capabilities, consume_person_memory_envelope +from .context_orchestration import contract_descriptor, contract_fingerprint LOCAL_TZ = ZoneInfo("Asia/Shanghai") @@ -42,6 +44,44 @@ class MemoryCompanionBridge: def __init__(self, plugin: Any): self._plugin = plugin + def _trusted_session_context(self, session_context: SessionContext | dict[str, Any] | None) -> SessionContext | dict[str, Any] | None: + """Normalize a legacy dict at the public bridge boundary. + + ``legacy_local`` is intentionally minted only here. Direct service + calls carrying a dict remain P3-unavailable, while this in-process + compatibility path remains confined to one verified session. + """ + if not isinstance(session_context, dict): + return session_context + normalizer = getattr(self._plugin, "session_context_from_bridge", None) + if not callable(normalizer): + return session_context + ctx = normalizer(dict(session_context)) + if not isinstance(ctx, SessionContext) or ctx.context_orchestration_status != "unavailable": + return ctx + has_p2_or_person_payload = any( + key in session_context and session_context[key] is not None + for key in ( + "unified_person_projection", "unified_person_source_identity", + "person_memory_envelope", "unified_person_id", "person_id", + ) + ) + if ( + not has_p2_or_person_payload + and ctx.scope in {"private", "group"} + and bool( + session_target_id(ctx.session_id, ctx.scope) + or clean_text(ctx.session_id, 200).startswith(f"{ctx.scope}:") + and clean_text(ctx.session_id, 200)[len(ctx.scope) + 1:] + ) + ): + ctx.strict_session_only = True + ctx.context_orchestration_status = "legacy_local" + marker = getattr(self._plugin, "_p3_mark_trusted_legacy_context", None) + if callable(marker): + marker(ctx) + return ctx + def probe_runtime_capabilities(self) -> dict[str, Any]: capabilities = getattr(self._plugin, "runtime_capabilities", None) if capabilities is None: @@ -68,6 +108,15 @@ class MemoryCompanionBridge: """Expose the read-only unified-person consumer contract.""" return contract_capabilities() + def probe_context_orchestration_capabilities(self) -> dict[str, Any]: + return { + "available": True, + "contract": contract_descriptor(), + "contract_fingerprint": contract_fingerprint(), + "bridge_requires_verified_core_for_high_risk": True, + "legacy_local_same_session_only": True, + } + def consume_unified_person_projection( self, projection: dict[str, Any] | None, @@ -561,7 +610,11 @@ class MemoryCompanionBridge: session_context: SessionContext | dict[str, Any] | None = None, top_k: int | None = None, ) -> list[dict[str, Any]]: - return await self._plugin.bridge_search(query, session_context=session_context, top_k=top_k) + return await self._plugin.bridge_search( + query, + session_context=self._trusted_session_context(session_context), + top_k=top_k, + ) def probe_archive_capabilities(self) -> dict[str, Any]: return self._plugin.bridge_probe_archive_capabilities() @@ -652,7 +705,7 @@ class MemoryCompanionBridge: ) -> str: return await self._plugin.bridge_compose_injection( query, - session_context=session_context, + session_context=self._trusted_session_context(session_context), top_k=top_k, max_chars=max_chars, companion_bot_mood=companion_bot_mood, @@ -689,7 +742,7 @@ class MemoryCompanionBridge: ) -> str: return await self._plugin.bridge_compose_context( query=query, - session_context=session_context, + session_context=self._trusted_session_context(session_context), top_k=top_k, max_chars=max_chars, companion_bot_mood=companion_bot_mood, diff --git a/core/context_orchestration.py b/core/context_orchestration.py new file mode 100644 index 0000000..e5ee47c --- /dev/null +++ b/core/context_orchestration.py @@ -0,0 +1,192 @@ +# -*- coding: utf-8 -*- +"""P3 versioned, metadata-only request context contract.""" +from __future__ import annotations + +import hashlib +import json +import re +from copy import deepcopy +from datetime import datetime, timezone +from typing import Any + +CONTRACT_NAME = "ops.context_orchestration.v1" +CONTRACT_VERSION = "1.0" +ATTRIBUTE_NAME = "_ops_context_orchestration_v1" +LAYERS = ("persona", "runtime", "person", "scene", "memory", "safety") +PHASES = ("initialized", "companion_ready", "memory_ready", "safety_ready", "finalized", "invalid") +SECURITY_STATUSES = ("allowed", "sanitized", "blocked", "quarantined", "unavailable") +POLICIES = ("inherit", "allow", "deny", "quarantine") +TRUST_LEVELS = ("T0", "T1", "T2", "T3", "T4") +CONTEXT_INIT, COMPANION_CONTEXT, TTS_FALLBACK, MEMORY_CONTEXT = 100, 0, -10, -20 +INCOMPATIBLE_TOOL, SENSITIVE_TOOL, FINALIZE = -20000, -21000, -22000 +MAX_FRAGMENTS, MAX_ERRORS, MAX_REASON_CODES = 64, 16, 16 +_FRAGMENT_FIELDS = ("key", "layer", "source", "trust_level", "content_hash", "chars", "status") +_CORE = ("persona", "runtime", "person", "scene") +_STATE_FIELDS = {"contract_name", "contract_version", "contract_fingerprint", "request_id", "session_id", "scope", "created_at", "phase", "completed_layers", "fragments", "security", "errors", "finalized"} +_SECURITY_FIELDS = {"status", "event_id", "memory_policy", "tool_policy", "response_policy", "reason_codes"} +_CODE_RE = re.compile(r"^[a-z0-9][a-z0-9_.:-]{0,119}$") + + +def _json(value: Any) -> str: + return json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":")) + + +def contract_descriptor(*, fixture_version: str = "1") -> dict[str, Any]: + return {"contract_name": CONTRACT_NAME, "contract_version": CONTRACT_VERSION, "attribute_name": ATTRIBUTE_NAME, "fixture_version": str(fixture_version), "layers": list(LAYERS), "phases": list(PHASES), "security_statuses": list(SECURITY_STATUSES), "policies": list(POLICIES), "trust_levels": list(TRUST_LEVELS), "state_fields": ["contract_name", "contract_version", "contract_fingerprint", "request_id", "session_id", "scope", "created_at", "phase", "completed_layers", "fragments", "security", "errors", "finalized"], "security_fields": ["status", "event_id", "memory_policy", "tool_policy", "response_policy", "reason_codes"], "fragment_fields": list(_FRAGMENT_FIELDS), "limits": {"fragments": MAX_FRAGMENTS, "errors": MAX_ERRORS, "reason_codes": MAX_REASON_CODES}} + + +def contract_fingerprint(*, fixture_version: str = "1") -> str: + return hashlib.sha256(_json(contract_descriptor(fixture_version=fixture_version)).encode("utf-8")).hexdigest() + + +def _text(value: Any, limit: int = 240) -> str: + return str(value or "").replace("\r", " ").replace("\n", " ").strip()[:limit] + + +def _utc_timestamp(value: Any = None) -> str: + if value is None: + return datetime.now(timezone.utc).isoformat(timespec="milliseconds").replace("+00:00", "Z") + if not isinstance(value, str) or value != value.strip() or "T" not in value or not value.endswith("Z"): + return "" + try: + datetime.fromisoformat(value[:-1] + "+00:00") + except ValueError: + return "" + return value + + +def _security() -> dict[str, Any]: + return {"status": "allowed", "event_id": "", "memory_policy": "inherit", "tool_policy": "inherit", "response_policy": "inherit", "reason_codes": []} + + +def new_context_state(*, request_id: Any = "", session_id: Any = "", scope: Any = "unknown", created_at: Any = None, fixture_version: str = "1") -> dict[str, Any]: + return {"contract_name": CONTRACT_NAME, "contract_version": CONTRACT_VERSION, "contract_fingerprint": contract_fingerprint(fixture_version=fixture_version), "request_id": _text(request_id, 180), "session_id": _text(session_id), "scope": _text(scope, 80) or "unknown", "created_at": _utc_timestamp(created_at), "phase": "initialized", "completed_layers": [], "fragments": [], "security": _security(), "errors": [], "finalized": False} + + +def _error(state: dict[str, Any], code: str) -> None: + errors = state.setdefault("errors", []) + if isinstance(errors, list) and code not in errors and len(errors) < MAX_ERRORS: + errors.append(code) + + +def _invalid(state: dict[str, Any], code: str) -> dict[str, Any]: + _error(state, code); state["phase"] = "invalid"; state["finalized"] = False + return state + + +def consume_context_state(value: Any, *, fixture_version: str = "1") -> dict[str, Any] | None: + if not isinstance(value, dict): return None + state = value + if state.get("contract_name") != CONTRACT_NAME or state.get("contract_version") != CONTRACT_VERSION: return _invalid(state, "contract_mismatch") + if state.get("contract_fingerprint") != contract_fingerprint(fixture_version=fixture_version): return _invalid(state, "fingerprint_mismatch") + if set(state) != _STATE_FIELDS or state.get("phase") not in PHASES or not isinstance(state.get("finalized"), bool): return _invalid(state, "state_invalid") + if not isinstance(state.get("created_at"), str) or not state["created_at"] or _utc_timestamp(state["created_at"]) != state["created_at"]: return _invalid(state, "created_at_invalid") + completed = state.get("completed_layers") + if not isinstance(completed, list): return _invalid(state, "state_invalid") + if any(layer not in LAYERS for layer in completed) or len(completed) != len(set(completed)) or completed != sorted(completed, key=LAYERS.index): return _invalid(state, "completed_layers_invalid") + if state["finalized"] != (state["phase"] == "finalized"): return _invalid(state, "finalized_phase_invalid") + if state["finalized"] and set(completed) != set(LAYERS): return _invalid(state, "finalized_layers_invalid") + if not isinstance(state.get("fragments"), list) or len(state["fragments"]) > MAX_FRAGMENTS: return _invalid(state, "fragments_invalid") + for fragment in state["fragments"]: + if not isinstance(fragment, dict) or set(fragment) != set(_FRAGMENT_FIELDS) or fragment.get("layer") not in LAYERS or fragment.get("trust_level") not in TRUST_LEVELS: return _invalid(state, "fragment_invalid") + security = state.get("security") + if not isinstance(security, dict) or set(security) != _SECURITY_FIELDS or security.get("status") not in SECURITY_STATUSES or any(security.get(key) not in POLICIES for key in ("memory_policy", "tool_policy", "response_policy")): return _invalid(state, "security_invalid") + errors = state.get("errors") + reason_codes = security.get("reason_codes") + if not isinstance(reason_codes, list) or len(reason_codes) > MAX_REASON_CODES or not isinstance(errors, list) or len(errors) > MAX_ERRORS: return _invalid(state, "limits_invalid") + if any(not isinstance(code, str) or _CODE_RE.fullmatch(code) is None for code in [*errors, *reason_codes]): return _invalid(state, "codes_invalid") + return state + + +def attach_context_state(state: dict[str, Any], request: Any | None = None, event: Any | None = None) -> dict[str, Any]: + for carrier in (request, event): + if carrier is not None: + try: setattr(carrier, ATTRIBUTE_NAME, state) + except Exception: pass + return state + + +def read_context_state(request: Any | None = None, event: Any | None = None, *, fixture_version: str = "1") -> dict[str, Any] | None: + for carrier in (request, event): + state = consume_context_state(getattr(carrier, ATTRIBUTE_NAME, None) if carrier is not None else None, fixture_version=fixture_version) + if state is not None: + attach_context_state(state, request, event) + return state + return None + + +def _refresh(state: dict[str, Any]) -> None: + if state["phase"] == "invalid" or state["finalized"]: return + completed = set(state["completed_layers"]) + state["phase"] = "safety_ready" if "safety" in completed else "memory_ready" if "memory" in completed else "companion_ready" if set(_CORE).issubset(completed) else "initialized" + + +def _open(state: dict[str, Any]) -> bool: + return consume_context_state(state) is not None and state.get("phase") != "invalid" and not state.get("finalized") + + +def complete_layer(state: dict[str, Any], layer: str) -> dict[str, Any]: + if not _open(state): return state + if layer not in LAYERS: return _invalid(state, "layer_invalid") + if layer not in state["completed_layers"]: + state["completed_layers"].append(layer) + state["completed_layers"].sort(key=LAYERS.index) + _refresh(state); return state + + +def register_fragment(state: dict[str, Any], *, key: Any, layer: str, source: Any = "", trust_level: str = "T2", content: Any = None, content_hash: Any = "", chars: Any = None, status: Any = "allowed") -> dict[str, Any]: + if not _open(state): return state + if layer not in LAYERS or trust_level not in TRUST_LEVELS: return _invalid(state, "fragment_invalid") + raw = "" if content is None else str(content) + try: size = max(0, int(len(raw) if chars is None else chars)) + except (TypeError, ValueError): return _invalid(state, "fragment_chars_invalid") + item = {"key": _text(key, 120) or "fragment", "layer": layer, "source": _text(source, 120), "trust_level": trust_level, "content_hash": _text(content_hash, 128) or hashlib.sha256(raw.encode("utf-8")).hexdigest(), "chars": size, "status": _text(status, 40) or "allowed"} + for old in state["fragments"]: + if old == item: return state + if old.get("key") == item["key"]: return _invalid(state, "fragment_key_conflict") + if len(state["fragments"]) >= MAX_FRAGMENTS: return _invalid(state, "fragment_limit") + state["fragments"].append(item); return state + + +def update_security(state: dict[str, Any], *, status: str | None = None, event_id: Any = None, memory_policy: str | None = None, tool_policy: str | None = None, response_policy: str | None = None, reason_codes: Any = None) -> dict[str, Any]: + if not _open(state): return state + security = state["security"] + if status is not None: + if status not in SECURITY_STATUSES: return _invalid(state, "security_status_invalid") + security["status"] = status + for key, value in (("memory_policy", memory_policy), ("tool_policy", tool_policy), ("response_policy", response_policy)): + if value is not None: + if value not in POLICIES: return _invalid(state, "security_policy_invalid") + security[key] = value + if event_id is not None: security["event_id"] = _text(event_id, 180) + codes = [reason_codes] if isinstance(reason_codes, str) else (reason_codes or []) + if not isinstance(codes, (list, tuple, set)): return _invalid(state, "reason_codes_invalid") + for code in codes: + if not isinstance(code, str) or _CODE_RE.fullmatch(code) is None: return _invalid(state, "reason_code_invalid") + if code not in security["reason_codes"] and len(security["reason_codes"]) < MAX_REASON_CODES: security["reason_codes"].append(code) + return state + + +def finalize_state(state: dict[str, Any]) -> dict[str, Any]: + if not _open(state): return state + if set(LAYERS).issubset(set(state["completed_layers"])): + state["phase"], state["finalized"] = "finalized", True + else: + _error(state, "finalize_pending_layers") + return state + + +def project_context_state(state: dict[str, Any] | None) -> dict[str, Any] | None: + state = consume_context_state(state) + if state is None: return None + return deepcopy({key: state[key] for key in ("contract_name", "contract_version", "contract_fingerprint", "request_id", "session_id", "scope", "created_at", "phase", "completed_layers", "fragments", "security", "errors", "finalized")}) + + +def infer_context_layer(key: Any = "", source: Any = "") -> str: + text = f"{key or ''} {source or ''}".lower() + if any(token in text for token in ("memory", "recall")): return "memory" + if any(token in text for token in ("safety", "guard", "tool", "tts", "privacy", "sanitize")): return "safety" + if any(token in text for token in ("persona", "reply.style", "identity", "character")): return "persona" + if any(token in text for token in ("private.context", "preferred_address", "preferred address", "relationship", "nickname", "person", "user.")): return "person" + if any(token in text for token in ("runtime", "state", "schedule", "temporal", "weather", "food", "agenda", "time")): return "runtime" + return "scene" diff --git a/core/models.py b/core/models.py index b9b9c1b..344f0a6 100644 --- a/core/models.py +++ b/core/models.py @@ -119,6 +119,9 @@ class SessionContext: person_id: str = "" person_origin_identity_key: str = "" person_identity_assurance: str = "" + context_orchestration: dict[str, Any] = field(default_factory=dict) + context_orchestration_status: str = "unavailable" + context_orchestration_error: str = "" @property def current_target_id(self) -> str: diff --git a/core/service.py b/core/service.py index 0801e3c..3046d6b 100644 --- a/core/service.py +++ b/core/service.py @@ -60,6 +60,11 @@ from .chat_import import HistoricalChatImporter from .classifier import MemoryClassifier from .config import ConfigView from .context_orchestrator import RetrievalIntent, RetrievalIntentBuilder +from .context_orchestration import ( + ATTRIBUTE_NAME, + attach_context_state, complete_layer, consume_context_state, project_context_state, + read_context_state, register_fragment, update_security, +) from .identity import IdentityResolver, looks_like_command, maybe_await, normalize_session_context_fields, session_target_id from .importance import ImportanceEvaluator from .injection import ( @@ -339,10 +344,174 @@ class MemoryCompanionService: async def rollback_historical_chat_import(self, batch_id: str) -> dict[str, Any]: return await self.chat_importer.rollback_batch(batch_id) + def _p3_mark_trusted_legacy_context(self, ctx: SessionContext) -> None: + registry = getattr(self, "_p3_trusted_legacy_contexts", None) + if not isinstance(registry, dict): + registry = {} + self._p3_trusted_legacy_contexts = registry + registry[id(ctx)] = ctx + + def _p3_take_trusted_legacy_context(self, ctx: SessionContext) -> bool: + registry = getattr(self, "_p3_trusted_legacy_contexts", {}) + if not isinstance(registry, dict) or registry.get(id(ctx)) is not ctx: + return False + registry.pop(id(ctx), None) + return True + + @staticmethod + def _p3_has_valid_session_id(ctx: SessionContext) -> bool: + session_id = clean_text(ctx.session_id, 200) + return bool( + session_target_id(session_id, ctx.scope) + or session_id.startswith(f"{ctx.scope}:") and session_id[len(ctx.scope) + 1:] + ) + + @staticmethod + def _p3_carrier_value(carrier: Any, key: str) -> Any: + if isinstance(carrier, dict): + return carrier.get(key) + return getattr(carrier, key, None) if carrier is not None else None + + def _p3_prepare_context(self, ctx: SessionContext, *, event: Any = None, req: Any = None) -> dict[str, Any] | None: + raw_present = any(self._p3_carrier_value(carrier, ATTRIBUTE_NAME) is not None for carrier in (req, event) if carrier is not None) + state = read_context_state(request=req, event=event) + if state is None and isinstance(ctx.context_orchestration, dict) and ctx.context_orchestration: + state = consume_context_state(ctx.context_orchestration) + if state is None: + ctx.strict_session_only = True + ctx.context_orchestration = {} + if raw_present: + ctx.context_orchestration_status = "invalid" + ctx.context_orchestration_error = "invalid_context_orchestration" + else: + ctx.context_orchestration_status = "unavailable" + ctx.context_orchestration_error = "" + return None + projection = project_context_state(state) + ctx.context_orchestration = projection or {} + if state.get("phase") == "invalid": + ctx.context_orchestration_status = "invalid" + ctx.context_orchestration_error = "invalid_context_orchestration" + else: + ctx.context_orchestration_status = "accepted" + ctx.context_orchestration_error = "" + return state + + def _p3_high_risk_context(self, ctx: SessionContext, *, event: Any = None, req: Any = None) -> bool: + if ctx.person_id or ctx.person_origin_identity_key or ctx.person_identity_assurance: + return True + for carrier in (req, event): + for key in ( + "unified_person_projection", "unified_person_source_identity", + "person_memory_envelope", "unified_person_id", "person_id", + ): + if self._p3_carrier_value(carrier, key): + return True + return False + + def _p3_gate_reason( + self, + ctx: SessionContext, + *, + event: Any = None, + req: Any = None, + state: dict[str, Any] | None = None, + high_risk: bool | None = None, + require_contract: bool = False, + ) -> str: + if state is None: + state = self._p3_prepare_context(ctx, event=event, req=req) + risk = self._p3_high_risk_context(ctx, event=event, req=req) if high_risk is None else bool(high_risk) + if state is None and ctx.context_orchestration_status == "invalid": + return "p3_invalid" + if state is None: + return "p3_unavailable" if require_contract or risk else "" + if state.get("phase") == "invalid": + return "p3_invalid" + security = state.get("security") if isinstance(state.get("security"), dict) else {} + status = str(security.get("status") or "unavailable") + if status in {"blocked", "quarantined", "unavailable"}: + return f"p3_security_{status}" + memory_policy = str(security.get("memory_policy") or "inherit") + if memory_policy in {"deny", "quarantine"}: + return f"p3_memory_policy_{memory_policy}" + if risk: + completed = set(state.get("completed_layers") or []) + if not {"persona", "runtime", "person", "scene"}.issubset(completed): + return "p3_core_incomplete" + if state.get("phase") not in {"companion_ready", "memory_ready", "safety_ready", "finalized"}: + return "p3_phase_not_ready" + return "" + + def _p3_bridge_gate_reason(self, ctx: SessionContext) -> str: + state = consume_context_state(ctx.context_orchestration) if ctx.context_orchestration else None + if state is None: + ctx.strict_session_only = True + trusted_legacy = ( + ctx.context_orchestration_status == "legacy_local" + and self._p3_take_trusted_legacy_context(ctx) + ) + if ( + trusted_legacy + and ctx.scope in {"private", "group"} + and self._p3_has_valid_session_id(ctx) + and not self._p3_high_risk_context(ctx) + ): + return "" + return "p3_invalid" if ctx.context_orchestration_status == "invalid" else "p3_unavailable" + reason = self._p3_gate_reason(ctx, state=state, high_risk=True, require_contract=True) + if reason: + return reason + if state.get("phase") != "companion_ready": + return "p3_phase_not_companion_ready" + return "" + + def _p3_mark_denied(self, state: dict[str, Any] | None, *, event: Any = None, req: Any = None, reason: str) -> None: + if state is not None: + update_security(state, memory_policy="deny", reason_codes=reason) + attach_context_state(state, request=req, event=event) + else: + carrier = event if event is not None else req + if carrier is not None: + try: + setattr(carrier, "_memory_companion_p3_gate_reason", reason) + except Exception: + pass + + def _p3_complete_memory(self, state: dict[str, Any] | None, *, ctx: SessionContext, injection: str = "", event: Any = None, req: Any = None) -> None: + if state is None or state.get("phase") in {"invalid", "finalized"}: + return + security = state.get("security") if isinstance(state.get("security"), dict) else {} + if security.get("status") in {"blocked", "quarantined", "unavailable"}: + return + if security.get("memory_policy") in {"deny", "quarantine"}: + return + completed = set(state.get("completed_layers") or []) + if not {"persona", "runtime", "person", "scene"}.issubset(completed): + return + injection_text = str(injection or "") + register_fragment( + state, key="memory.injection", layer="memory", source="memory_companion", + trust_level="T1", content_hash=hashlib.sha256(injection_text.encode("utf-8")).hexdigest(), + chars=len(injection_text), status=str(security.get("status") or "allowed"), + ) + complete_layer(state, "memory") + attach_context_state(state, request=req, event=event) + ctx.context_orchestration = project_context_state(state) or {} + ctx.context_orchestration_status = "accepted" + async def handle_llm_request(self, event: Any, req: Any) -> None: ctx = await self.identity.resolve_event_context(event) self._sanitize_session_context_message_text(ctx) self._apply_private_companion_preferred_address(ctx, req=req, event=event) + p3_state = self._p3_prepare_context(ctx, event=event, req=req) + p3_reason = self._p3_gate_reason(ctx, event=event, req=req, state=p3_state) + if p3_reason: + remove_temp_text(req, MEMORY_COMPANION_INJECTION_HEADER, MEMORY_COMPANION_INJECTION_FOOTER) + self._mark_memory_companion_injection_state(event, req, injected=False, conversation_memory=False, slot_map={}) + self._p3_mark_denied(p3_state, event=event, req=req, reason=p3_reason) + logger.warning("[MemoryCompanion] P3 request denied: session=%s reason=%s", ctx.session_id, p3_reason) + return if self._private_companion_internal_generation_event(event): return if looks_like_command(ctx.message_text): @@ -364,7 +533,9 @@ class MemoryCompanionService: # this memory plugin no longer advances relationship authority. try: - await self.inject_memories(ctx, req, event=event) + injection = await self.inject_memories(ctx, req, event=event) + if injection is not None: + self._p3_complete_memory(p3_state, ctx=ctx, injection=injection, event=event, req=req) except Exception as exc: logger.warning( "[MemoryCompanion] 记忆注入失败,已放行本轮 LLM 请求: session=%s error=%s", @@ -440,6 +611,11 @@ class MemoryCompanionService: return ctx = await self.identity.resolve_event_context(event) self._sanitize_session_context_message_text(ctx) + p3_state = self._p3_prepare_context(ctx, event=event) + p3_reason = self._p3_gate_reason(ctx, event=event, state=p3_state) + if p3_reason: + self._p3_mark_denied(p3_state, event=event, reason=p3_reason) + return if ctx.scope != "group": return if looks_like_command(ctx.message_text): @@ -493,6 +669,11 @@ class MemoryCompanionService: return ctx = await self.identity.resolve_event_context(event) + p3_state = self._p3_prepare_context(ctx, event=event) + p3_reason = self._p3_gate_reason(ctx, event=event, state=p3_state) + if p3_reason: + self._p3_mark_denied(p3_state, event=event, reason=p3_reason) + return if looks_like_command(ctx.message_text): return record = self.classifier.from_bot_response(ctx, text) @@ -699,6 +880,22 @@ class MemoryCompanionService: metadata: dict[str, Any] | None = None, occurred_at: str = "", ) -> str: + event_metadata = dict(metadata or {}) + p3_raw = event_metadata.get("context_orchestration") + p3_state = consume_context_state(p3_raw) if p3_raw is not None else None + gate_ctx = SessionContext( + session_id=clean_text(session_id, 200), scope=clean_text(scope, 40), + platform=clean_text(platform, 80), user_id=clean_text(user_id, 120), + group_id=clean_text(group_id, 120), context_orchestration=project_context_state(p3_state) or {}, + ) + high_risk = gate_ctx.scope == "group" or bool(event_metadata.get("unified_person_projection") or event_metadata.get("person_memory_envelope")) + reason = self._p3_gate_reason(gate_ctx, state=p3_state, high_risk=high_risk, require_contract=p3_raw is not None) + if reason: + return "" + if p3_state is not None: + event_metadata["context_orchestration"] = project_context_state(p3_state) + else: + event_metadata.pop("context_orchestration", None) text = self._sanitize_visible_timeline_text(content) if not text: return "" @@ -712,7 +909,6 @@ class MemoryCompanionService: event_type = "user_message" subject_id = clean_text(user_id, 120) object_id = clean_text(group_id if scope == "group" else user_id, 120) - event_metadata = dict(metadata or {}) if source: event_metadata.setdefault("source", clean_text(source, 80)) event_metadata.setdefault("timezone", clean_text(event_metadata.get("timezone"), 80) or "Asia/Shanghai") @@ -797,6 +993,23 @@ class MemoryCompanionService: if not self.config.bool("private_companion_bridge.accept_external_records", True): raise RuntimeError("外部记息写入已关闭") + metadata = dict(kwargs.get("metadata") or {}) if isinstance(kwargs.get("metadata"), dict) else {} + kwargs["metadata"] = metadata + p3_raw = metadata.get("context_orchestration") + p3_state = consume_context_state(p3_raw) if p3_raw is not None else None + gate_ctx = SessionContext( + session_id=clean_text(kwargs.get("session_id"), 200), scope=clean_text(kwargs.get("scope"), 40), + platform=clean_text(kwargs.get("platform"), 80), group_id=clean_text(kwargs.get("group_id"), 120), + context_orchestration=project_context_state(p3_state) or {}, + ) + high_risk = gate_ctx.scope == "group" or bool(metadata.get("unified_person_projection") or metadata.get("person_memory_envelope") or metadata.get("person_id")) + reason = self._p3_gate_reason(gate_ctx, state=p3_state, high_risk=high_risk, require_contract=p3_raw is not None) + if reason: + raise UserMemoryBoundaryError(reason) + if p3_state is not None: + metadata["context_orchestration"] = project_context_state(p3_state) + else: + metadata.pop("context_orchestration", None) explicit_id = clean_text(kwargs.pop("memory_id", "") or kwargs.pop("id", ""), 120) record = self.classifier.external_record(**kwargs) memory_domain = clean_text(getattr(record, "memory_domain", ""), 60).lower() @@ -1071,6 +1284,8 @@ class MemoryCompanionService: top_k: int | None = None, ) -> list[dict[str, Any]]: ctx = self.session_context_from_bridge(session_context) + if self._p3_bridge_gate_reason(ctx): + return [] results = await self.search(query, ctx, top_k or self.config.int("memory_injection.top_k", 6)) return [serialize_memory(item.memory, item.score, item.reason) for item in results] @@ -1232,6 +1447,10 @@ class MemoryCompanionService: ) -> dict[str, Any]: ctx = self._archive_context(scope) query_text = clean_text(query, 1400) + gate_reason = self._p3_gate_reason( + ctx, state=consume_context_state(ctx.context_orchestration) if ctx.context_orchestration else None, + high_risk=True, require_contract=True, + ) base_response = { "archive_api_version": ARCHIVE_API_VERSION, "scope": await self.bridge_get_archive_scope(scope=scope), @@ -1239,6 +1458,9 @@ class MemoryCompanionService: "excluded": [], "warnings": [], } + if gate_reason: + base_response["warnings"] = [gate_reason] + return base_response if not query_text: base_response["error"] = "empty_query_denied" base_response["warnings"] = ["archive search requires a non-empty query"] @@ -1294,6 +1516,16 @@ class MemoryCompanionService: include_timeline: bool = True, ) -> dict[str, Any]: ctx = self._archive_context(scope) + gate_reason = self._p3_gate_reason( + ctx, state=consume_context_state(ctx.context_orchestration) if ctx.context_orchestration else None, + high_risk=True, require_contract=True, + ) + if gate_reason: + return { + "archive_api_version": "1.0", + "scope": await self.bridge_get_archive_scope(scope=scope), + "memories": [], "excluded": [], "warnings": [gate_reason], + } policy = self.archive_visibility_policy() requested = [clean_text(item, 200) for item in memory_ids if clean_text(item, 200)] records = await self.store.get_memories_by_ids([item for item in requested if not item.startswith("timeline_")]) @@ -1350,8 +1582,21 @@ class MemoryCompanionService: "archive_api_version": ARCHIVE_API_VERSION, } ctx = self._archive_context(scope) + archive_state = consume_context_state(ctx.context_orchestration) if ctx.context_orchestration else None + gate_reason = self._p3_gate_reason( + ctx, state=archive_state, high_risk=True, require_contract=True, + ) + if gate_reason: + return { + "ok": False, + "error": gate_reason, + "archive_api_version": ARCHIVE_API_VERSION, + "memory_id": "", + "operation_id": operation, + } safe_metadata = dict(metadata or {}) safe_metadata.update({"source": "memory_archivist", "operation_id": operation}) + safe_metadata["context_orchestration"] = project_context_state(archive_state) memory_id = await self.record_external_event( content=text, memory_type="archive_summary", @@ -1384,6 +1629,17 @@ class MemoryCompanionService: include_timeline: bool = True, ) -> dict[str, Any]: ctx = self._archive_context(scope) + gate_reason = self._p3_gate_reason( + ctx, state=consume_context_state(ctx.context_orchestration) if ctx.context_orchestration else None, + high_risk=True, require_contract=True, + ) + if gate_reason: + return { + "archive_api_version": "1.0", + "scope": await self.bridge_get_archive_scope(scope=scope), + "start_at": clean_text(start_at, 80), "end_at": clean_text(end_at, 80), + "items": [], "warnings": [gate_reason], + } policy = self.archive_visibility_policy() items: list[dict[str, Any]] = [] if include_timeline: @@ -1440,6 +1696,8 @@ class MemoryCompanionService: companion_bot_energy: float = 0.0, ) -> str: ctx = self.session_context_from_bridge(session_context) + if self._p3_bridge_gate_reason(ctx): + return "" query_text = clean_text(query or ctx.message_text, 1400) if not query_text: return "" @@ -1508,6 +1766,8 @@ class MemoryCompanionService: ) -> str: started = time.monotonic() ctx = self._normalized_session_context(self.session_context_from_bridge(session_context)) + if self._p3_bridge_gate_reason(ctx): + return "" query_text = clean_text(query or ctx.message_text, 1400) if not query_text: return "" @@ -1749,52 +2009,8 @@ class MemoryCompanionService: async def bridge_search_open_loops(self, *, session_id: str = "", limit: int = 3) -> list[dict[str, Any]]: """Search for unresolved open-loop / promise memories for proactive companionship.""" - ctx = SessionContext(session_id=session_id or "", scope="private") - try: - results = await self.search( - "约定 承诺 下次 继续 没完成 待续 还没 回头", - ctx, - top_k=max(limit * 3, 6), - ) - except Exception: - return [] - open_loops: list[dict[str, Any]] = [] - now = time.time() - for item in results: - memory = item.memory - metadata = memory.metadata if isinstance(memory.metadata, dict) else {} - try: - open_loop_w = float(metadata.get("open_loop_weight") or 0.0) - promise_w = float(metadata.get("promise_weight") or 0.0) - except Exception: - open_loop_w = promise_w = 0.0 - if max(open_loop_w, promise_w) < 0.30: - continue - if metadata.get("resolved_at"): - continue - occurred = clean_text(memory.occurred_at or memory.created_at, 40) - age_days = None - if occurred: - try: - dt = datetime.fromisoformat(occurred.replace("Z", "+00:00")) - age_days = (now - dt.timestamp()) / 86400.0 - except Exception: - pass - if age_days is not None and age_days < 0.5: - continue - open_loops.append({ - "memory_id": memory.id, - "content": clean_text(memory.content, 300), - "session_id": memory.session_id, - "occurred_at": occurred, - "age_days": round(age_days, 1) if age_days is not None else None, - "open_loop_weight": round(open_loop_w, 3), - "promise_weight": round(promise_w, 3), - "memory_reason": clean_text(metadata.get("memory_reason"), 200), - }) - if len(open_loops) >= limit: - break - return open_loops + # This legacy API has no carrier for the verified P3 contract. + return [] def _detect_and_queue_emotional_events( self, @@ -2994,6 +3210,9 @@ class MemoryCompanionService: person_id=ctx.person_id, person_origin_identity_key=ctx.person_origin_identity_key, person_identity_assurance=ctx.person_identity_assurance, + context_orchestration=deepcopy(ctx.context_orchestration), + context_orchestration_status=ctx.context_orchestration_status, + context_orchestration_error=ctx.context_orchestration_error, ) def _bot_entity(self, ctx: SessionContext | None = None) -> EntityRef: @@ -3006,6 +3225,8 @@ class MemoryCompanionService: def _schedule_session_summary(self, ctx: SessionContext, *, reason: str) -> None: if not self.config.bool("memory_summary.enabled", True): return + if self._p3_gate_reason(ctx, state=consume_context_state(ctx.context_orchestration) if ctx.context_orchestration else None): + return snapshot = self._snapshot_context(ctx) self._spawn_background(self._background_summarize_session(snapshot, reason), label=f"summary:{reason}") @@ -3094,6 +3315,8 @@ class MemoryCompanionService: return min(base * (2**exponent), cooldown) if cooldown > 0 else base * (2**exponent) async def maybe_summarize_session(self, ctx: SessionContext, *, force: bool = False) -> str: + if self._p3_gate_reason(ctx, state=consume_context_state(ctx.context_orchestration) if ctx.context_orchestration else None): + return "" if not force and not self.config.bool("memory_summary.enabled", True): return "" if not ctx.session_id: @@ -3705,6 +3928,9 @@ class MemoryCompanionService: async def search_for_event(self, event: Any, query: str, top_k: int = 6): ctx = await self.identity.resolve_event_context(event) + state = self._p3_prepare_context(ctx, event=event) + if self._p3_gate_reason(ctx, event=event, state=state): + return [] time_intent = parse_time_intent(query or ctx.message_text) retrieval_query = self._query_for_time_intent(query, time_intent) return await self.search( @@ -3716,6 +3942,9 @@ class MemoryCompanionService: async def explain_for_event(self, event: Any, query: str, top_k: int = 6): ctx = await self.identity.resolve_event_context(event) + state = self._p3_prepare_context(ctx, event=event) + if self._p3_gate_reason(ctx, event=event, state=state): + return [], [] time_intent = parse_time_intent(query or ctx.message_text) retrieval_query = self._query_for_time_intent(query, time_intent) return await self.search_with_diagnostics( @@ -3727,6 +3956,11 @@ class MemoryCompanionService: async def explain_context_for_event(self, event: Any, query: str, top_k: int = 6): 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) + if reason: + intent = self.intent_builder.build(ctx, event=event, explicit_query=query) + return intent, [], [{"reason": reason}], {} time_intent = parse_time_intent(query or ctx.message_text) intent = self.intent_builder.build( ctx, @@ -3748,6 +3982,9 @@ class MemoryCompanionService: async def add_manual_memory(self, event: Any, content: str) -> str: ctx = await self.identity.resolve_event_context(event) + state = self._p3_prepare_context(ctx, event=event) + if self._p3_gate_reason(ctx, event=event, state=state): + return "" visibility = "internal" if ctx.scope == "private": visibility = "private_pair" @@ -3780,6 +4017,10 @@ class MemoryCompanionService: async def tool_remember(self, event: Any, content: str, *, note_type: str = "memory") -> dict[str, Any]: 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) + if reason: + return {"ok": False, "error": reason} content = clean_text(content, 3000) note_type = clean_text(note_type, 40) or "memory" if not content: @@ -3815,6 +4056,10 @@ class MemoryCompanionService: async def tool_recall(self, event: Any, query: str, top_k: int = 5) -> dict[str, Any]: 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) + if reason: + return {"ok": False, "error": reason, "memories": []} query = clean_text(query, 1000) if not query: return {"ok": False, "error": "empty query", "memories": []} @@ -4867,7 +5112,13 @@ class MemoryCompanionService: ) return injection - async def inject_memories(self, ctx: SessionContext, req: Any, *, event: Any = None) -> None: + async def inject_memories(self, ctx: SessionContext, req: Any, *, event: Any = None) -> str | None: + state = self._p3_prepare_context(ctx, event=event, req=req) + reason = self._p3_gate_reason(ctx, event=event, req=req, state=state) + if reason: + remove_temp_text(req, MEMORY_COMPANION_INJECTION_HEADER, MEMORY_COMPANION_INJECTION_FOOTER) + self._mark_memory_companion_injection_state(event, req, injected=False, conversation_memory=False, slot_map={}) + return None removed = remove_temp_text(req, MEMORY_COMPANION_INJECTION_HEADER, MEMORY_COMPANION_INJECTION_FOOTER) if removed: logger.info("[MemoryCompanion] 已清理历史记忆包注入片段: session=%s count=%s", ctx.session_id, removed) @@ -4875,7 +5126,7 @@ class MemoryCompanionService: self._mark_memory_companion_injection_state(event, req, injected=False, conversation_memory=False, slot_map={}) if self.config.bool("memory_injection.debug_log_injection_enabled", False): logger.info("[MemoryCompanion] 记忆注入已关闭: session=%s", ctx.session_id) - return + return "" self._sanitize_request_history_for_companion(ctx, req) recent_fact_context = await self._recent_fact_guard_context(ctx) recent_cross_window_context = await self._recent_cross_window_context(ctx) @@ -4997,10 +5248,10 @@ class MemoryCompanionService: slot_map={}, ) if append_temp_text(req, injection): - return + return injection prompt = clean_text(getattr(req, "prompt", "") or "", 8000) req.prompt = f"{prompt}\n\n{injection}" if prompt else injection - return + return injection blocked: list[dict[str, Any]] = [] retrieval_path_info: dict[str, Any] = { "mode": clean_text(self.config.get("retrieval.mode", "auto"), 40), @@ -5153,7 +5404,7 @@ class MemoryCompanionService: ) if not injection: self._mark_memory_companion_injection_state(event, req, injected=False, conversation_memory=False, slot_map=slot_map) - return + return "" self._mark_memory_companion_injection_state( event, @@ -5170,12 +5421,13 @@ class MemoryCompanionService: len(results), len(injection), ) - return + return injection prompt = clean_text(getattr(req, "prompt", "") or "", 8000) req.prompt = f"{prompt}\n\n{injection}" if prompt else injection logger.warning("[MemoryCompanion] TextPart 不可用,已回退到 prompt 注入: session=%s", ctx.session_id) + return injection def _log_injection_debug( self, *, @@ -7779,6 +8031,8 @@ class MemoryCompanionService: if isinstance(session_context, SessionContext): return self._normalized_session_context(session_context) payload = session_context or {} + p3_raw = payload.get("context_orchestration") + p3_state = consume_context_state(p3_raw) if p3_raw is not None else None normalized = normalize_session_context_fields( session_id=str(payload.get("session_id") or ""), scope=str(payload.get("scope") or "unknown"), @@ -7800,6 +8054,9 @@ class MemoryCompanionService: strict_session_only=bool(payload.get("strict_session_only", False)), preferred_address=str(payload.get("preferred_address") or ""), preferred_address_locked=bool(payload.get("preferred_address_locked", False)), + context_orchestration=project_context_state(p3_state) or {}, + context_orchestration_status=("accepted" if p3_state and p3_state.get("phase") != "invalid" else ("invalid" if p3_raw is not None else "unavailable")), + context_orchestration_error=("invalid_context_orchestration" if p3_raw is not None and (not p3_state or p3_state.get("phase") == "invalid") else ""), ) from .unified_person_contract import consume_unified_person_projection resolved = consume_unified_person_projection( @@ -7819,7 +8076,7 @@ class MemoryCompanionService: user_id=ctx.user_id, group_id=ctx.group_id, ) - return SessionContext( + result = SessionContext( session_id=normalized["session_id"], scope=normalized["scope"], platform=normalized["platform"], @@ -7836,7 +8093,13 @@ class MemoryCompanionService: person_id=ctx.person_id, person_origin_identity_key=ctx.person_origin_identity_key, person_identity_assurance=ctx.person_identity_assurance, + context_orchestration=deepcopy(ctx.context_orchestration), + context_orchestration_status=ctx.context_orchestration_status, + context_orchestration_error=ctx.context_orchestration_error, ) + if self._p3_take_trusted_legacy_context(ctx): + self._p3_mark_trusted_legacy_context(result) + return result def stable_id(self, *parts: Any) -> str: raw = "|".join(clean_text(part, 500) for part in parts if part is not None) diff --git a/main.py b/main.py index f4f5e2b..66177e5 100644 --- a/main.py +++ b/main.py @@ -11,6 +11,7 @@ from astrbot.api.provider import LLMResponse, ProviderRequest from astrbot.api.star import Context, Star, StarTools, register 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.service import MemoryCompanionService @@ -85,7 +86,7 @@ class MemoryCompanionPlugin(Star): self.page_api = None logger.warning("[MemoryCompanion] 拓展页 API 注册失败: %s", exc, exc_info=True) - @filter.on_llm_request(priority=-20) + @filter.on_llm_request(priority=HOOK_PRIORITY_MEMORY_CONTEXT) async def on_llm_request(self, event: AstrMessageEvent, req: ProviderRequest): await self.service.handle_llm_request(event, req) diff --git a/tests/fixtures/context_orchestration_v1.json b/tests/fixtures/context_orchestration_v1.json new file mode 100644 index 0000000..09e833f --- /dev/null +++ b/tests/fixtures/context_orchestration_v1.json @@ -0,0 +1 @@ +{"contract_name":"ops.context_orchestration.v1","contract_version":"1.0","attribute_name":"_ops_context_orchestration_v1","fixture_version":"1","layers":["persona","runtime","person","scene","memory","safety"],"phases":["initialized","companion_ready","memory_ready","safety_ready","finalized","invalid"],"security_statuses":["allowed","sanitized","blocked","quarantined","unavailable"],"policies":["inherit","allow","deny","quarantine"],"trust_levels":["T0","T1","T2","T3","T4"],"state_fields":["contract_name","contract_version","contract_fingerprint","request_id","session_id","scope","created_at","phase","completed_layers","fragments","security","errors","finalized"],"security_fields":["status","event_id","memory_policy","tool_policy","response_policy","reason_codes"],"fragment_fields":["key","layer","source","trust_level","content_hash","chars","status"],"limits":{"fragments":64,"errors":16,"reason_codes":16}} diff --git a/tests/test_p3_context_orchestration.py b/tests/test_p3_context_orchestration.py new file mode 100644 index 0000000..5450349 --- /dev/null +++ b/tests/test_p3_context_orchestration.py @@ -0,0 +1,268 @@ +from __future__ import annotations + +import asyncio +import hashlib +import json +import unittest +from pathlib import Path +from types import SimpleNamespace + +from core import context_orchestration as contract +from core.bridge import MemoryCompanionBridge +from core.models import SessionContext +from core.service import MemoryCompanionService + + +def ready_state() -> dict: + state = contract.new_context_state(request_id="r", session_id="private:a", scope="private") + for layer in ("persona", "runtime", "person", "scene"): + contract.complete_layer(state, layer) + return state + + +class _Identity: + def __init__(self, ctx: SessionContext): + self.ctx = ctx + + async def resolve_event_context(self, event): + return self.ctx + + +class _Config: + def bool(self, key, default=False): + return True + + +class _GateService(MemoryCompanionService): + def __init__(self, ctx: SessionContext): + self.identity = _Identity(ctx) + self.config = _Config() + self.reached_write_path = False + + def _sanitize_session_context_message_text(self, ctx): + return None + + def _apply_private_companion_preferred_address(self, ctx, **kwargs): + return None + + def _private_companion_internal_generation_event(self, event): + return False + + def _mark_memory_companion_injection_state(self, *args, **kwargs): + return None + + async def note_identity(self, ctx): + self.reached_write_path = True + + +class _BridgeProbe: + def __init__(self, service: MemoryCompanionService): + self.service = service + self.received: SessionContext | None = None + self.gate_reason = "" + + def session_context_from_bridge(self, payload): + return self.service.session_context_from_bridge(payload) + + def _p3_mark_trusted_legacy_context(self, ctx): + self.service._p3_mark_trusted_legacy_context(ctx) + + async def bridge_search(self, query, *, session_context=None, top_k=None): + self.received = self.service.session_context_from_bridge(session_context) + self.gate_reason = self.service._p3_bridge_gate_reason(self.received) + return ["same-session"] if not self.gate_reason else [] + + +class P3MemoryTests(unittest.TestCase): + def setUp(self) -> None: + self.service = MemoryCompanionService.__new__(MemoryCompanionService) + + def test_fixture_descriptor_and_fingerprint(self): + fixture = json.loads((Path(__file__).parent / "fixtures" / "context_orchestration_v1.json").read_text(encoding="utf-8")) + descriptor = contract.contract_descriptor() + self.assertEqual(fixture, descriptor) + self.assertEqual(len(contract.contract_fingerprint()), 64) + self.assertEqual(contract.MEMORY_CONTEXT, -20) + + def test_created_at_is_valid_projected_and_immutable(self): + fixed = "2026-07-29T00:00:00.000Z" + state = contract.new_context_state(created_at=fixed) + contract.complete_layer(state, "persona") + self.assertEqual(state["created_at"], fixed) + self.assertEqual(contract.project_context_state(state)["created_at"], fixed) + invalid = contract.project_context_state(state) or {} + invalid["created_at"] = "" + self.assertEqual(contract.consume_context_state(invalid)["phase"], "invalid") + + def test_contract_rejects_unknown_fields_limits_order_and_finalized_mismatch(self): + unknown = ready_state() + unknown["raw_content"] = "must not cross the contract" + self.assertEqual(contract.consume_context_state(unknown)["phase"], "invalid") + too_many_errors = ready_state() + too_many_errors["errors"] = ["x"] * 17 + self.assertEqual(contract.consume_context_state(too_many_errors)["phase"], "invalid") + finalized_mismatch = ready_state() + finalized_mismatch["phase"] = "finalized" + self.assertEqual(contract.consume_context_state(finalized_mismatch)["phase"], "invalid") + wrong_order = contract.new_context_state() + wrong_order["completed_layers"] = ["scene", "persona"] + self.assertEqual(contract.consume_context_state(wrong_order)["phase"], "invalid") + canonical = contract.new_context_state() + contract.complete_layer(canonical, "scene") + contract.complete_layer(canonical, "persona") + self.assertEqual(canonical["completed_layers"], ["persona", "scene"]) + + def test_controlled_codes_reject_untrusted_values_without_coercion(self): + accepted = contract.new_context_state() + contract.update_security(accepted, reason_codes="a" * 120) + self.assertEqual(accepted["security"]["reason_codes"], ["a" * 120]) + for value in ("", "a" * 121, "bad code", "bad\ncode", 7): + state = contract.new_context_state() + contract.update_security(state, reason_codes=[value]) + self.assertEqual(state["phase"], "invalid") + self.assertIn("reason_code_invalid", state["errors"]) + malformed = contract.new_context_state() + malformed["security"]["reason_codes"] = ["raw prose"] + self.assertEqual(contract.consume_context_state(malformed)["phase"], "invalid") + + def test_gate_security_and_person_completion(self): + ctx = SessionContext(session_id="private:a", scope="private", person_id="person:a") + self.assertEqual(self.service._p3_gate_reason(ctx, state=None, high_risk=True), "p3_unavailable") + state = contract.new_context_state(session_id=ctx.session_id, scope=ctx.scope) + self.assertEqual(self.service._p3_gate_reason(ctx, state=state, high_risk=True), "p3_core_incomplete") + for layer in ("persona", "runtime", "person", "scene"): + contract.complete_layer(state, layer) + self.assertEqual(self.service._p3_gate_reason(ctx, state=state, high_risk=True), "") + for status in ("blocked", "quarantined", "unavailable"): + blocked = ready_state(); contract.update_security(blocked, status=status) + self.assertEqual(self.service._p3_gate_reason(ctx, state=blocked, high_risk=True), f"p3_security_{status}") + denied = ready_state(); contract.update_security(denied, memory_policy="deny") + self.assertEqual(self.service._p3_gate_reason(ctx, state=denied, high_risk=True), "p3_memory_policy_deny") + + def test_memory_outcome_is_metadata_only_and_idempotent(self): + state = ready_state() + ctx = SessionContext(session_id="private:a", scope="private") + req, event = SimpleNamespace(), SimpleNamespace() + injection = "actual memory injection" + self.service._p3_complete_memory(state, ctx=ctx, injection=injection, event=event, req=req) + self.service._p3_complete_memory(state, ctx=ctx, injection=injection, event=event, req=req) + self.assertEqual(state["phase"], "memory_ready") + memory_fragments = [item for item in state["fragments"] if item["layer"] == "memory"] + self.assertEqual(len(memory_fragments), 1) + self.assertNotIn("content", memory_fragments[0]) + self.assertEqual(memory_fragments[0]["chars"], len(injection)) + self.assertEqual(memory_fragments[0]["content_hash"], hashlib.sha256(injection.encode("utf-8")).hexdigest()) + self.assertIs(contract.read_context_state(req, event), state) + + def test_bridge_context_consumes_bad_contract_and_strict_flag_cannot_forge_person(self): + bad = ready_state(); bad["contract_version"] = "bad" + ctx = self.service.session_context_from_bridge({ + "session_id": "private:a", "scope": "private", "strict_session_only": True, + "context_orchestration": bad, + }) + self.assertEqual(ctx.context_orchestration_status, "invalid") + person_ctx = SessionContext(session_id="private:a", scope="private", strict_session_only=True, person_id="person:a") + self.assertEqual(self.service._p3_gate_reason(person_ctx, state=None), "p3_unavailable") + + def test_bridge_gate_limits_legacy_to_same_session_and_requires_companion_ready(self): + legacy = self.service.session_context_from_bridge({ + "session_id": "private:a", "scope": "private", "strict_session_only": False, + }) + self.assertEqual(legacy.context_orchestration_status, "unavailable") + self.assertEqual(self.service._p3_bridge_gate_reason(legacy), "p3_unavailable") + person = SessionContext(session_id="private:a", scope="private", person_id="person:a") + self.assertEqual(self.service._p3_bridge_gate_reason(person), "p3_unavailable") + forged = SessionContext( + session_id="private:a", scope="private", context_orchestration_status="legacy_local", + ) + self.assertEqual(self.service._p3_bridge_gate_reason(forged), "p3_unavailable") + state = ready_state() + accepted = SessionContext( + session_id="private:a", scope="private", + context_orchestration=contract.project_context_state(state) or {}, + context_orchestration_status="accepted", + ) + self.assertEqual(self.service._p3_bridge_gate_reason(accepted), "") + contract.complete_layer(state, "memory") + accepted.context_orchestration = contract.project_context_state(state) or {} + self.assertEqual(self.service._p3_bridge_gate_reason(accepted), "p3_phase_not_companion_ready") + + def test_public_bridge_mints_legacy_only_for_low_risk_valid_session(self): + probe = _BridgeProbe(self.service) + bridge = MemoryCompanionBridge(probe) + result = asyncio.run(bridge.search( + "hello", session_context={"session_id": "private:a", "scope": "private"}, + )) + self.assertEqual(result, ["same-session"]) + self.assertEqual(probe.gate_reason, "") + self.assertIsNotNone(probe.received) + self.assertEqual(probe.received.context_orchestration_status, "legacy_local") + self.assertTrue(probe.received.strict_session_only) + self.assertEqual(getattr(self.service, "_p3_trusted_legacy_contexts", {}), {}) + + rejected = asyncio.run(bridge.search( + "hello", + session_context={ + "session_id": "private:a", "scope": "private", + "unified_person_projection": {"person_id": "person:a"}, + }, + )) + self.assertEqual(rejected, []) + self.assertEqual(probe.gate_reason, "p3_unavailable") + + bad = ready_state(); bad["contract_version"] = "bad" + rejected = asyncio.run(bridge.search( + "hello", + session_context={"session_id": "private:a", "scope": "private", "context_orchestration": bad}, + )) + self.assertEqual(rejected, []) + self.assertEqual(probe.gate_reason, "p3_invalid") + + def test_archive_requires_contract(self): + result = asyncio.run(self.service.bridge_search_archive_memories( + "x", scope={"session_id": "private:a", "scope": "private", "user_id": "a"}, limit=1, + )) + self.assertEqual(result["memories"], []) + self.assertIn("p3_unavailable", result["warnings"]) + self.service.config = _Config() + create = asyncio.run(self.service.bridge_create_archive_memory( + scope={"session_id": "private:a", "scope": "private", "user_id": "a"}, + content="archive", metadata={}, confirmed_by="owner", operation_id="op-1", + )) + self.assertEqual(create["error"], "p3_unavailable") + self.assertEqual(create["memory_id"], "") + + def test_blocked_request_group_response_and_external_write_stop_before_storage(self): + state = ready_state(); contract.update_security(state, status="blocked") + event = SimpleNamespace() + req = SimpleNamespace(system_prompt="", prompt="") + contract.attach_context_state(state, req, event) + service = _GateService(SessionContext(session_id="private:a", scope="private", message_text="hello")) + asyncio.run(service.handle_llm_request(event, req)) + self.assertFalse(service.reached_write_path) + self.assertEqual(state["security"]["memory_policy"], "deny") + + group_state = ready_state(); contract.update_security(group_state, status="quarantined") + group_event = SimpleNamespace(); contract.attach_context_state(group_state, event=group_event) + group_service = _GateService(SessionContext(session_id="group:g", scope="group", group_id="g", user_id="u", message_text="hello")) + asyncio.run(group_service.handle_group_message(group_event)) + self.assertFalse(group_service.reached_write_path) + + response_state = ready_state(); contract.update_security(response_state, status="unavailable") + response_event = SimpleNamespace(); contract.attach_context_state(response_state, event=response_event) + response_service = _GateService(SessionContext(session_id="private:a", scope="private", message_text="hello")) + asyncio.run(response_service.handle_llm_response(response_event, SimpleNamespace(role="assistant", completion_text="reply"))) + self.assertFalse(response_service.reached_write_path) + + external = MemoryCompanionService.__new__(MemoryCompanionService) + external.config = _Config() + with self.assertRaises(Exception): + asyncio.run(external.record_external_event( + content="blocked", memory_type="user_fact", memory_domain="user_memory", + scope="private", session_id="private:a", + metadata={"context_orchestration": response_state}, + )) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_p4_memory_boundary.py b/tests/test_p4_memory_boundary.py new file mode 100644 index 0000000..5140750 --- /dev/null +++ b/tests/test_p4_memory_boundary.py @@ -0,0 +1,154 @@ +from __future__ import annotations + +import json +from dataclasses import fields +from pathlib import Path + +from core import context_orchestration +from core.bridge import MemoryCompanionBridge +from core.models import MemoryRecord, SessionContext +from core.service import MemoryCompanionService +from core.store import MemoryStore + + +FIXTURE_PATH = Path(__file__).parent / "fixtures" / "unified_person_projection_v1.json" +SOURCE_IDENTITY = { + "companion_instance_id": "companion-a", + "bot_account_id": "bot-1", + "adapter_instance_id": "telegram-main", + "subject_namespace": "telegram.user", + "platform_subject_id": "42", +} + +# These fields belong to P4's companion-owned affinity/confinement state +# machine. A memory-side transport object or persistence schema must not +# acquire them as authority-bearing state. +P4_STATE_FIELDS = frozenset( + { + "affinity_score", + "affinity_band", + "warning_stage", + "confinement_state", + "confinement_until", + "confinement_round", + "release_prompt_pending", + "affinity_event_id", + "violation_event_id", + } +) +P4_AUTHORITY_TERMS = ("affinity", "confinement", "warning", "release", "settle", "recompute") + + +def _projection() -> dict: + return json.loads(FIXTURE_PATH.read_text(encoding="utf-8")) + + +def _service() -> MemoryCompanionService: + return MemoryCompanionService.__new__(MemoryCompanionService) + + +def _ready_p3_state() -> dict: + state = context_orchestration.new_context_state(session_id="private:42", scope="private") + for layer in ("persona", "runtime", "person", "scene"): + context_orchestration.complete_layer(state, layer) + return state + + +def test_bridge_reduces_verified_projection_to_identity_only_session_context(): + bridge = MemoryCompanionBridge(_service()) + context = bridge._trusted_session_context( + { + "session_id": "private:42", + "scope": "private", + "unified_person_projection": _projection(), + "unified_person_source_identity": SOURCE_IDENTITY, + } + ) + + assert isinstance(context, SessionContext) + assert context.person_id == "person_d301001be7a1ce40df26a3eb" + assert context.person_origin_identity_key == _projection()["resolved_identity_key"] + assert context.person_identity_assurance == "verified" + assert context.context_orchestration == {} + assert context.context_orchestration_status == "unavailable" + + session_fields = {item.name for item in fields(SessionContext)} + memory_fields = {item.name for item in fields(MemoryRecord)} + assert P4_STATE_FIELDS.isdisjoint(session_fields) + assert P4_STATE_FIELDS.isdisjoint(memory_fields) + assert all(not hasattr(context, field) for field in P4_STATE_FIELDS) + + +def test_public_memory_bridge_exposes_no_p4_authority_operation(): + public_operations = { + name + for name in dir(MemoryCompanionBridge) + if not name.startswith("_") and callable(getattr(MemoryCompanionBridge, name)) + } + authority_operations = { + name + for name in public_operations + if any(term in name.lower() for term in P4_AUTHORITY_TERMS) + } + + assert authority_operations == set() + + +def test_memory_schema_has_no_p4_affinity_or_confinement_authority(tmp_path): + store = MemoryStore(tmp_path / "memory.db") + store.initialize() + try: + table_names = { + str(row["name"]) + for row in store._conn.execute("SELECT name FROM sqlite_master WHERE type='table'").fetchall() + } + p4_tables = { + name + for name in table_names + if any(term in name.lower() for term in ("affinity", "confinement", "release")) + } + schema_columns: set[str] = set() + for table_name in table_names: + quoted_name = table_name.replace('"', '""') + schema_columns.update( + str(row["name"]) + for row in store._conn.execute(f'PRAGMA table_info("{quoted_name}")').fetchall() + ) + + assert p4_tables == set() + assert P4_STATE_FIELDS.isdisjoint(schema_columns) + finally: + store.close() + + +def test_p4_shaped_inputs_fail_closed_at_p2_and_p3_boundaries(): + service = _service() + + invalid_projection = _projection() + invalid_projection["affinity_score"] = -1201 + invalid_projection["affinity_band"] = "critical" + p2_context = service.session_context_from_bridge( + { + "session_id": "private:42", + "scope": "private", + "unified_person_projection": invalid_projection, + "unified_person_source_identity": SOURCE_IDENTITY, + } + ) + assert p2_context.person_id == "" + assert p2_context.person_origin_identity_key == "" + assert p2_context.person_identity_assurance == "" + + p3_state = _ready_p3_state() + p3_state["release_prompt_pending"] = True + p3_context = service.session_context_from_bridge( + { + "session_id": "private:42", + "scope": "private", + "context_orchestration": p3_state, + } + ) + assert p3_context.context_orchestration_status == "invalid" + assert p3_context.context_orchestration["phase"] == "invalid" + assert "release_prompt_pending" not in p3_context.context_orchestration + assert service._p3_bridge_gate_reason(p3_context) == "p3_invalid" diff --git a/tests/test_summary_relationship.py b/tests/test_summary_relationship.py index 8e61c3f..1f94953 100644 --- a/tests/test_summary_relationship.py +++ b/tests/test_summary_relationship.py @@ -13,6 +13,7 @@ ROOT = Path(__file__).resolve().parents[1] if str(ROOT.parent) not in sys.path: sys.path.insert(0, str(ROOT.parent)) +from core import context_orchestration as p3_contract from core.bridge import MemoryCompanionBridge from core.models import EntityRef, MemoryRecord, SearchResult, SessionContext from core.service import MemoryCompanionService @@ -164,9 +165,13 @@ class SummaryAndRelationshipTests(unittest.IsolatedAsyncioTestCase): raise AssertionError("generic retrieval engine must not run for schedule_fast") service._retrieval_engine = fail_if_called - text = await service.bridge_compose_context( + bridge = MemoryCompanionBridge(service) + text = await bridge.compose_context( query=ctx.message_text, - session_context=ctx, + session_context={ + "session_id": ctx.session_id, "scope": ctx.scope, "user_id": ctx.user_id, + "bot_id": ctx.bot_id, "message_text": ctx.message_text, + }, top_k=5, max_chars=1200, retrieval_profile="schedule_fast", @@ -367,9 +372,13 @@ class SummaryAndRelationshipTests(unittest.IsolatedAsyncioTestCase): max_chars=1800, recent_fact_context=recent_context, ) - bridged = await service.bridge_compose_injection( + bridge = MemoryCompanionBridge(service) + bridged = await bridge.compose_injection( "不是这样", - session_context=ctx, + session_context={ + "session_id": ctx.session_id, "scope": ctx.scope, "user_id": ctx.user_id, + "bot_id": ctx.bot_id, "message_text": ctx.message_text, + }, max_chars=1800, ) @@ -497,6 +506,11 @@ class SummaryAndRelationshipTests(unittest.IsolatedAsyncioTestCase): user_id="u1", message_text="今日穿搭、历史穿搭、服装偏好和最近自拍", ) + p3_state = p3_contract.new_context_state(session_id=ctx.session_id, scope=ctx.scope) + for layer in ("persona", "runtime", "person", "scene"): + p3_contract.complete_layer(p3_state, layer) + ctx.context_orchestration = p3_contract.project_context_state(p3_state) or {} + ctx.context_orchestration_status = "accepted" records = [ MemoryRecord( id="outfit-schedule",