From 5c8cffdf060577193d30f3973c6eb4b47a25d257 Mon Sep 17 00:00:00 2001 From: YokiiroBW Date: Tue, 28 Jul 2026 01:40:34 +0800 Subject: [PATCH] feat: complete unified person P0-P2 memory layer --- core/bridge.py | 157 ++++++ core/identity.py | 11 +- core/models.py | 41 ++ core/person_memory.py | 353 ++++++++++++ core/person_memory_contract.py | 89 +++ core/retrieval.py | 4 + core/service.py | 107 +++- core/store.py | 94 +++- core/unified_person_contract.py | 290 ++++++++++ core/unified_person_persistence.py | 525 ++++++++++++++++++ docs/统一人物系统开发回执-20260727.md | 88 +++ docs/错题本.md | 51 ++ .../unified_person_projection_v1.json | 25 + tests/test_person_memory_p2.py | 303 ++++++++++ tests/test_unified_person_contract.py | 154 +++++ tests/test_unified_person_persistence.py | 308 ++++++++++ 16 files changed, 2579 insertions(+), 21 deletions(-) create mode 100644 core/person_memory.py create mode 100644 core/person_memory_contract.py create mode 100644 core/unified_person_contract.py create mode 100644 core/unified_person_persistence.py create mode 100644 docs/统一人物系统开发回执-20260727.md create mode 100644 tests/fixtures/unified_person_projection_v1.json create mode 100644 tests/test_person_memory_p2.py create mode 100644 tests/test_unified_person_contract.py create mode 100644 tests/test_unified_person_persistence.py diff --git a/core/bridge.py b/core/bridge.py index c5216aa..f0a7728 100644 --- a/core/bridge.py +++ b/core/bridge.py @@ -8,6 +8,11 @@ from .models import EntityRef, MemoryRecord, SessionContext, clean_text 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 +from .unified_person_contract import ( + consume_unified_person_projection, + contract_capabilities, +) +from .person_memory_contract import capabilities as person_memory_capabilities, consume_person_memory_envelope LOCAL_TZ = ZoneInfo("Asia/Shanghai") @@ -59,6 +64,158 @@ class MemoryCompanionBridge: def probe_ops_contract(self) -> dict[str, Any]: return OpsDomainContract.to_dict() + def probe_unified_person_contract(self) -> dict[str, Any]: + """Expose the read-only unified-person consumer contract.""" + return contract_capabilities() + + def consume_unified_person_projection( + self, + projection: dict[str, Any] | None, + *, + source_identity: dict[str, Any] | None = None, + companion_available: bool = True, + ) -> dict[str, Any]: + """Validate a companion projection without creating or changing a person.""" + return consume_unified_person_projection( + projection, + source_identity=source_identity, + companion_available=companion_available, + ) + + def probe_unified_person_persistence_capabilities(self) -> dict[str, Any]: + method_map = { + "coordinate": "bridge_coordinate_unified_person_reference", + "claim_pending": "bridge_claim_unified_person_coordination", + "replay_claim": "bridge_replay_unified_person_coordination", + "migration_preview": "preview_unified_person_reference_migration", + "migration_apply": "apply_unified_person_reference_migration", + "migration_rollback": "rollback_unified_person_reference_migration", + } + operation_availability = { + operation: callable(getattr(self._plugin, method_name, None)) + for operation, method_name in method_map.items() + } + return { + "available": all(operation_availability.values()), + "schema_version": 1, + "operations": list(method_map), + "operation_availability": operation_availability, + "queue_statuses": ["resolved", "pending_companion", "contract_mismatch", "identity_conflict", "quarantined", "legacy_unresolved"], + } + + def probe_person_memory_capabilities(self) -> dict[str, Any]: + methods = { + "migration_preview": "preview_person_memory_migration", "migration_apply": "apply_person_memory_migration", + "migration_rollback": "rollback_person_memory_migration", "correction": "bridge_correct_person_memory", + "correction_rollback": "bridge_rollback_person_memory_correction", + } + operation_availability = {name: callable(getattr(self._plugin, method, None)) for name, method in methods.items()} + available = all(operation_availability.values()) + result = person_memory_capabilities(available=available, warnings=[] if available else ["bridge_method_unavailable"]) + result["operations"] = list(methods); result["operation_availability"] = operation_availability + return result + + def consume_person_memory_envelope(self, envelope: dict[str, Any] | None, *, companion_available: bool = True) -> dict[str, Any]: + return consume_person_memory_envelope(envelope, companion_available=companion_available) + + async def preview_person_memory_migration(self, *, limit: int = 5000) -> dict[str, Any]: + getter = getattr(self._plugin, "preview_person_memory_migration", None) + if not callable(getter): return {"ok": False, "error_code": "bridge_method_unavailable"} + return await getter(limit=limit) + + async def apply_person_memory_migration(self, *, migration_id: str = "", limit: int = 5000) -> dict[str, Any]: + getter = getattr(self._plugin, "apply_person_memory_migration", None) + if not callable(getter): return {"ok": False, "error_code": "bridge_method_unavailable"} + return await getter(migration_id=migration_id, limit=limit) + + async def rollback_person_memory_migration(self, migration_id: str) -> dict[str, Any]: + getter = getattr(self._plugin, "rollback_person_memory_migration", None) + if not callable(getter): return {"ok": False, "error_code": "bridge_method_unavailable"} + return await getter(migration_id) + + async def correct_person_memory(self, *, memory_id: str, correction_event_id: str, replacement: MemoryRecord) -> dict[str, Any]: + getter = getattr(self._plugin, "bridge_correct_person_memory", None) + if not callable(getter): return {"ok": False, "error_code": "bridge_method_unavailable"} + result = await getter(memory_id=memory_id, correction_event_id=correction_event_id, replacement=replacement) + return result if isinstance(result, dict) else {"ok": False, "error_code": "invalid_bridge_response"} + + async def rollback_person_memory_correction(self, correction_event_id: str) -> dict[str, Any]: + getter = getattr(self._plugin, "bridge_rollback_person_memory_correction", None) + if not callable(getter): return {"ok": False, "error_code": "bridge_method_unavailable"} + result = await getter(correction_event_id) + return result if isinstance(result, dict) else {"ok": False, "error_code": "invalid_bridge_response"} + + async def coordinate_unified_person_reference( + self, + *, + memory_id: str, + projection: dict[str, Any] | None, + source_identity: dict[str, Any] | None, + companion_available: bool = True, + operation_id: str = "", + ) -> dict[str, Any]: + getter = getattr(self._plugin, "bridge_coordinate_unified_person_reference", None) + if not callable(getter): + return {"ok": False, "status": "pending_companion", "error_code": "bridge_method_unavailable", "person_id": ""} + return await getter( + memory_id=memory_id, + projection=projection, + source_identity=source_identity, + companion_available=companion_available, + operation_id=operation_id, + ) + + async def claim_unified_person_coordination(self, *, limit: int = 20) -> dict[str, Any]: + getter = getattr(self._plugin, "bridge_claim_unified_person_coordination", None) + if not callable(getter): + return {"ok": False, "status": "pending_companion", "error_code": "bridge_method_unavailable", "items": []} + result = await getter(limit=limit) + if not isinstance(result, list): + return {"ok": False, "status": "quarantined", "error_code": "invalid_bridge_response", "items": []} + return {"ok": True, "status": "resolved", "error_code": "", "items": result} + + async def replay_unified_person_coordination( + self, + *, + queue_id: str, + lease_token: str, + projection: dict[str, Any] | None, + source_identity: dict[str, Any] | None, + companion_available: bool = True, + ) -> dict[str, Any]: + getter = getattr(self._plugin, "bridge_replay_unified_person_coordination", None) + if not callable(getter): + return {"ok": False, "status": "pending_companion", "error_code": "bridge_method_unavailable", "person_id": ""} + result = await getter( + queue_id=queue_id, + lease_token=lease_token, + projection=projection, + source_identity=source_identity, + companion_available=companion_available, + ) + return result if isinstance(result, dict) else {"ok": False, "status": "quarantined", "error_code": "invalid_bridge_response", "person_id": ""} + + async def preview_unified_person_reference_migration(self, *, limit: int = 5000) -> dict[str, Any]: + getter = getattr(self._plugin, "preview_unified_person_reference_migration", None) + if not callable(getter): + return {"mode": "preview", "ok": False, "error_code": "bridge_method_unavailable", "items": []} + result = await getter(limit=limit) + return result if isinstance(result, dict) else {"mode": "preview", "ok": False, "error_code": "invalid_bridge_response", "items": []} + + async def apply_unified_person_reference_migration(self, *, migration_id: str, limit: int = 5000) -> dict[str, Any]: + getter = getattr(self._plugin, "apply_unified_person_reference_migration", None) + if not callable(getter): + return {"mode": "apply", "ok": False, "migration_id": migration_id, "error_code": "bridge_method_unavailable", "changed": 0} + result = await getter(migration_id=migration_id, limit=limit) + return result if isinstance(result, dict) else {"mode": "apply", "ok": False, "migration_id": migration_id, "error_code": "invalid_bridge_response", "changed": 0} + + async def rollback_unified_person_reference_migration(self, migration_id: str) -> dict[str, Any]: + getter = getattr(self._plugin, "rollback_unified_person_reference_migration", None) + if not callable(getter): + return {"mode": "rollback", "ok": False, "migration_id": migration_id, "error_code": "bridge_method_unavailable", "restored": 0} + result = await getter(migration_id) + return result if isinstance(result, dict) else {"mode": "rollback", "ok": False, "migration_id": migration_id, "error_code": "invalid_bridge_response", "restored": 0} + def probe_calendar_contract(self) -> dict[str, Any]: return { "version": "1.0", diff --git a/core/identity.py b/core/identity.py index 935bb91..5604273 100644 --- a/core/identity.py +++ b/core/identity.py @@ -5,6 +5,7 @@ import re from typing import Any from .models import EntityRef, SessionContext, clean_text +from .unified_person_contract import consume_unified_person_projection async def maybe_await(value: Any) -> Any: @@ -123,7 +124,7 @@ class IdentityResolver: if scope == "unknown" and user_id: scope = "private" - return SessionContext( + ctx = SessionContext( session_id=session_id, scope=scope, platform=clean_text(platform, 80), @@ -135,6 +136,14 @@ class IdentityResolver: message_id=clean_text(message_id, 120), message_text=clean_text(text, 2000), ) + projection = getattr(event, "unified_person_projection", None) + source = getattr(event, "unified_person_source_identity", None) + resolved = consume_unified_person_projection(projection, source_identity=source) if projection is not None else {} + if resolved.get("accepted"): + ctx.person_id = clean_text(resolved.get("person_id"), 160) + ctx.person_origin_identity_key = clean_text(resolved.get("origin_identity_key"), 160) + ctx.person_identity_assurance = clean_text(resolved.get("projection", {}).get("identity_assurance"), 40) + return ctx async def _call(self, event: Any, name: str) -> str: func = getattr(event, name, None) diff --git a/core/models.py b/core/models.py index 431f586..b9b9c1b 100644 --- a/core/models.py +++ b/core/models.py @@ -114,6 +114,11 @@ class SessionContext: strict_session_only: bool = False preferred_address: str = "" preferred_address_locked: bool = False + # Set only by a verified companion projection/bridge path. Empty means + # ordinary session identity, never a synthesized person authority. + person_id: str = "" + person_origin_identity_key: str = "" + person_identity_assurance: str = "" @property def current_target_id(self) -> str: @@ -162,6 +167,18 @@ class MemoryRecord: content_fingerprint: str = "" merged_count: int = 1 supersedes_id: str = "" + person_id: str = "" + origin_identity_key: str = "" + coordination_status: str = "pending_companion" + projection_revision: int = 0 + fact_status: str = "pending" + observed_person_id: str = "" + origin_scope: str = "" + correction_event_id: str = "" + projection_event_id: str = "" + firewall_status: str = "allowed" + firewall_event_id: str = "" + fact_event_id: str = "" def ensure_defaults(self) -> "MemoryRecord": now = utc_now() @@ -255,6 +272,18 @@ class MemoryRecord: "content_fingerprint": self.content_fingerprint, "merged_count": self.merged_count, "supersedes_id": self.supersedes_id, + "person_id": self.person_id, + "origin_identity_key": self.origin_identity_key, + "coordination_status": self.coordination_status, + "projection_revision": int(self.projection_revision or 0), + "fact_status": self.fact_status, + "observed_person_id": self.observed_person_id, + "origin_scope": self.origin_scope, + "correction_event_id": self.correction_event_id, + "projection_event_id": self.projection_event_id, + "firewall_status": self.firewall_status, + "firewall_event_id": self.firewall_event_id, + "fact_event_id": self.fact_event_id, } @classmethod @@ -292,6 +321,18 @@ class MemoryRecord: content_fingerprint=get("content_fingerprint"), merged_count=int(get("merged_count") or 1), supersedes_id=get("supersedes_id"), + person_id=get("person_id"), + origin_identity_key=get("origin_identity_key"), + coordination_status=get("coordination_status"), + projection_revision=int(get("projection_revision") or 0), + fact_status=get("fact_status"), + observed_person_id=get("observed_person_id"), + origin_scope=get("origin_scope"), + correction_event_id=get("correction_event_id"), + projection_event_id=get("projection_event_id"), + firewall_status=get("firewall_status"), + firewall_event_id=get("firewall_event_id"), + fact_event_id=get("fact_event_id"), ) diff --git a/core/person_memory.py b/core/person_memory.py new file mode 100644 index 0000000..000533a --- /dev/null +++ b/core/person_memory.py @@ -0,0 +1,353 @@ +"""P2 person-memory lifecycle, firewall policy, and additive migration.""" +from __future__ import annotations + +import uuid +from collections.abc import Mapping +from typing import Any + +from .models import MemoryRecord, clean_text, json_dumps, json_loads, utc_now +from .person_memory_contract import consume_person_memory_envelope +from .unified_person_contract import origin_identity_key + + +P2_MIGRATION_VERSION = 2 +NON_INDEXABLE_FACT_STATUSES = frozenset({"superseded", "rejected", "quarantined"}) +NON_INDEXABLE_FIREWALL_STATUSES = frozenset({"blocked", "rejected", "quarantined"}) + + +def ensure_person_memory_schema(connection: Any) -> None: + existing = {row["name"] for row in connection.execute("PRAGMA table_info(memories)").fetchall()} + additions = { + "fact_status": "TEXT NOT NULL DEFAULT 'legacy'", + "observed_person_id": "TEXT NOT NULL DEFAULT ''", + "origin_scope": "TEXT NOT NULL DEFAULT 'legacy'", + "correction_event_id": "TEXT NOT NULL DEFAULT ''", + "projection_event_id": "TEXT NOT NULL DEFAULT ''", + "firewall_status": "TEXT NOT NULL DEFAULT 'allowed'", + "firewall_event_id": "TEXT NOT NULL DEFAULT ''", + "fact_event_id": "TEXT NOT NULL DEFAULT ''", + } + for name, ddl in additions.items(): + if name not in existing: + connection.execute(f"ALTER TABLE memories ADD COLUMN {name} {ddl}") + connection.executescript(""" + CREATE TABLE IF NOT EXISTS person_memory_migration_runs ( + migration_id TEXT PRIMARY KEY, migration_version INTEGER NOT NULL, + state TEXT NOT NULL, backup_path TEXT NOT NULL DEFAULT '', + created_at TEXT NOT NULL, updated_at TEXT NOT NULL + ); + CREATE TABLE IF NOT EXISTS person_memory_migration_backup ( + migration_id TEXT NOT NULL, memory_id TEXT NOT NULL, + fact_status TEXT NOT NULL, observed_person_id TEXT NOT NULL, + origin_scope TEXT NOT NULL, correction_event_id TEXT NOT NULL, + projection_event_id TEXT NOT NULL, firewall_status TEXT NOT NULL, + firewall_event_id TEXT NOT NULL, created_at TEXT NOT NULL, + PRIMARY KEY(migration_id, memory_id) + ); + CREATE INDEX IF NOT EXISTS idx_memories_person_fact_visibility + ON memories(person_id, observed_person_id, origin_scope, fact_status, firewall_status); + CREATE UNIQUE INDEX IF NOT EXISTS idx_memories_person_correction_event + ON memories(correction_event_id) WHERE correction_event_id!=''; + CREATE UNIQUE INDEX IF NOT EXISTS idx_memories_person_fact_event + ON memories(fact_event_id) WHERE fact_event_id!=''; + CREATE TABLE IF NOT EXISTS person_memory_correction_backup ( + correction_event_id TEXT PRIMARY KEY, original_memory_id TEXT NOT NULL, + replacement_memory_id TEXT NOT NULL, original_fact_status TEXT NOT NULL, + created_at TEXT NOT NULL + ); + CREATE TABLE IF NOT EXISTS person_memory_correction_derived_backup ( + correction_event_id TEXT NOT NULL, memory_id TEXT NOT NULL, + fact_status TEXT NOT NULL, lifecycle TEXT NOT NULL, + PRIMARY KEY(correction_event_id, memory_id) + ); + CREATE TABLE IF NOT EXISTS person_memory_correction_artifact_backup ( + correction_event_id TEXT NOT NULL, artifact_type TEXT NOT NULL, + payload TEXT NOT NULL, PRIMARY KEY(correction_event_id, artifact_type, payload) + ); + CREATE TRIGGER IF NOT EXISTS trg_memories_observed_person_immutable + BEFORE UPDATE OF observed_person_id ON memories + WHEN OLD.observed_person_id!='' AND NEW.observed_person_id!='' AND NEW.observed_person_id!=OLD.observed_person_id + BEGIN SELECT RAISE(ABORT, 'observed_person_id_immutable'); END; + """) + + +def _scope_for(record: MemoryRecord) -> str: + return "group" if record.scope == "group" or record.group_id else "private" if record.scope == "private" else "legacy" + + +def normalize_record(record: MemoryRecord) -> MemoryRecord: + record.fact_status = clean_text(getattr(record, "fact_status", ""), 40) or "pending" + record.firewall_status = clean_text(getattr(record, "firewall_status", ""), 40) or "allowed" + record.origin_scope = clean_text(getattr(record, "origin_scope", ""), 40) or _scope_for(record) + if record.fact_status not in {"pending", "confirmed", "superseded", "rejected", "quarantined", "legacy"}: + record.fact_status = "quarantined" + if record.firewall_status not in {"allowed", "blocked", "rejected", "quarantined"}: + record.firewall_status = "quarantined" + if record.firewall_status in NON_INDEXABLE_FIREWALL_STATUSES: + record.fact_status = "quarantined" if record.firewall_status == "quarantined" else "rejected" + record.review_status = "pending" if record.firewall_status == "quarantined" else "rejected" + record.lifecycle = "archived" + elif record.fact_status == "pending": + # Ordinary local writes have passed the P2 text firewall. They become + # visible facts; cross-person writes remain pending until companion P1 + # coordination resolves them. + record.fact_status = "pending" if record.person_id and record.coordination_status != "resolved" else "confirmed" + if record.person_id and not record.observed_person_id: + record.observed_person_id = record.person_id + return record + + +def apply_envelope(record: MemoryRecord) -> MemoryRecord: + """Consume a supplied P2 decision at the one Store write gate.""" + metadata = record.metadata if isinstance(record.metadata, dict) else {} + envelope = metadata.get("person_memory_envelope") + if envelope is None: + return record + result = consume_person_memory_envelope(envelope) + if not result.get("accepted"): + record.firewall_status = "quarantined" + record.firewall_event_id = clean_text(metadata.get("person_memory_event_id"), 160) + return record + event = result["envelope"] + record.fact_status = event["fact_status"] + record.firewall_status = event["firewall_status"] + record.origin_scope = event["origin_scope"] + record.correction_event_id = event["correction_event_id"] + record.projection_event_id = event["projection_event_id"] + record.firewall_event_id = event["firewall_event_id"] or event["event_id"] + record.fact_event_id = event["event_id"] + source = event.get("source_identity") + if source is not None and not record.origin_identity_key: + try: record.origin_identity_key = origin_identity_key(source) + except ValueError: + record.firewall_status = "quarantined" + projection = event.get("p0_projection") + if isinstance(projection, Mapping): + incoming_person = clean_text(projection.get("person_id"), 160) + incoming_revision = int(projection.get("projection_revision") or 0) + if not incoming_person or incoming_revision < 1: + record.firewall_status = "quarantined" + return record + # P2 only reprojects the current companion-owned reference. Historical + # observed/origin/evidence evidence is never rewritten by merge/split. + if record.origin_identity_key and source is not None: + try: + if record.origin_identity_key != origin_identity_key(source): + record.firewall_status = "quarantined" + return record + except ValueError: + record.firewall_status = "quarantined" + return record + record.person_id = incoming_person + record.projection_revision = max(int(record.projection_revision or 0), incoming_revision) + record.coordination_status = "resolved" + if not record.observed_person_id: + record.observed_person_id = incoming_person + return record + + +def indexable(record: MemoryRecord) -> bool: + return clean_text(record.fact_status, 40) not in NON_INDEXABLE_FACT_STATUSES and clean_text(record.firewall_status, 40) not in NON_INDEXABLE_FIREWALL_STATUSES + + +def visibility_reason(record: MemoryRecord, ctx: Any) -> str: + if not indexable(record): + return "person_memory_firewall" + if record.fact_status != "confirmed" and record.fact_status != "legacy" and not ( + record.fact_status == "pending" and not record.person_id and not record.origin_scope + ): + return "person_memory_not_confirmed" + if record.person_id and record.person_id != clean_text(getattr(ctx, "person_id", ""), 160): + return "person_memory_cross_person" + cross_channel = bool(record.person_id) and record.platform and record.platform != clean_text(getattr(ctx, "platform", ""), 80) + origins_differ = bool(record.person_id) and record.origin_identity_key and record.origin_identity_key != clean_text(getattr(ctx, "person_origin_identity_key", ""), 160) + if cross_channel or origins_differ: + assurance = clean_text(getattr(ctx, "person_identity_assurance", ""), 40) + if assurance not in {"verified", "explicit_linked"} or not record.origin_identity_key or not clean_text(getattr(ctx, "person_origin_identity_key", ""), 160): + return "person_memory_low_assurance_cross_channel" + if record.person_id and record.origin_scope == "group" and getattr(ctx, "scope", "") != "group": + return "person_memory_cross_scope" + if record.person_id and record.origin_scope == "private" and getattr(ctx, "scope", "") == "group": + return "person_memory_private_to_group" + if record.person_id and record.scope == "group" and record.group_id and record.group_id != clean_text(getattr(ctx, "group_id", ""), 160): + return "person_memory_cross_group" + return "" + + +class PersonMemoryMigration: + def __init__(self, store: Any): self.store = store + + async def preview(self, *, limit: int = 5000) -> dict[str, Any]: + return await self.store._run_recoverable_database_operation(self._preview_sync, limit) + + def _preview_sync(self, limit: int) -> dict[str, Any]: + rows = self.store._conn.execute("SELECT id FROM memories WHERE fact_status='legacy' ORDER BY id LIMIT ?", (max(1, int(limit)),)).fetchall() + return {"ok": True, "migration_version": P2_MIGRATION_VERSION, "count": len(rows), "memory_ids": [row["id"] for row in rows]} + + async def apply(self, *, migration_id: str = "", limit: int = 5000, interrupt_after: int = 0) -> dict[str, Any]: + return await self.store._run_recoverable_database_operation(self._apply_sync, migration_id, limit, interrupt_after) + + def _apply_sync(self, migration_id: str, limit: int, interrupt_after: int) -> dict[str, Any]: + migration_id = clean_text(migration_id, 120) or f"p2_{uuid.uuid4().hex}" + now = utc_now() + with self.store._lock: + completed = self.store._conn.execute("SELECT state FROM person_memory_migration_runs WHERE migration_id=?", (migration_id,)).fetchone() + if completed and completed["state"] == "completed": return {"ok": True, "migration_id": migration_id, "deduplicated": True} + backup_path = str(self.store.backup("before_person_memory_p2")) + try: + with self.store._transaction_sync(): + self.store._conn.execute("INSERT OR REPLACE INTO person_memory_migration_runs VALUES(?,?,?,?,?,?)", (migration_id, P2_MIGRATION_VERSION, "running", backup_path, now, now)) + rows = self.store._conn.execute("SELECT * FROM memories WHERE fact_status='legacy' ORDER BY id LIMIT ?", (max(1, int(limit)),)).fetchall() + for index, row in enumerate(rows, 1): + self.store._conn.execute("INSERT OR IGNORE INTO person_memory_migration_backup VALUES(?,?,?,?,?,?,?,?,?,?)", (migration_id, row['id'], row['fact_status'], row['observed_person_id'], row['origin_scope'], row['correction_event_id'], row['projection_event_id'], row['firewall_status'], row['firewall_event_id'], now)) + status = "confirmed" if row['person_id'] and row['coordination_status'] == 'resolved' else "pending" + self.store._conn.execute("UPDATE memories SET fact_status=?, observed_person_id=CASE WHEN observed_person_id='' THEN person_id ELSE observed_person_id END, origin_scope=CASE WHEN scope='group' OR group_id!='' THEN 'group' WHEN scope='private' THEN 'private' ELSE 'legacy' END, updated_at=? WHERE id=?", (status, now, row['id'])) + if interrupt_after and index >= interrupt_after: raise RuntimeError("p2_migration_interrupted") + self.store._conn.execute("UPDATE person_memory_migration_runs SET state='completed', updated_at=? WHERE migration_id=?", (utc_now(), migration_id)) + except RuntimeError as error: + if str(error) == "p2_migration_interrupted": + return {"ok": False, "migration_id": migration_id, "error_code": "interrupted", "backup_path": backup_path} + raise + return {"ok": True, "migration_id": migration_id, "count": len(rows), "deduplicated": False} + + async def rollback(self, migration_id: str) -> dict[str, Any]: + return await self.store._run_recoverable_database_operation(self._rollback_sync, migration_id) + + def _rollback_sync(self, migration_id: str) -> dict[str, Any]: + with self.store._lock: + with self.store._transaction_sync(): + rows = self.store._conn.execute("SELECT * FROM person_memory_migration_backup WHERE migration_id=?", (clean_text(migration_id, 120),)).fetchall() + for row in rows: + self.store._conn.execute("UPDATE memories SET fact_status=?, observed_person_id=?, origin_scope=?, correction_event_id=?, projection_event_id=?, firewall_status=?, firewall_event_id=?, updated_at=? WHERE id=?", (*[row[key] for key in ('fact_status','observed_person_id','origin_scope','correction_event_id','projection_event_id','firewall_status','firewall_event_id')], utc_now(), row['memory_id'])) + self.store._conn.execute("UPDATE person_memory_migration_runs SET state='rolled_back', updated_at=? WHERE migration_id=?", (utc_now(), clean_text(migration_id, 120))) + return {"ok": True, "migration_id": migration_id, "restored": len(rows), "schema_retained": True} + + +class PersonMemoryFacts: + """Idempotent correction lifecycle without rewriting original evidence.""" + def __init__(self, store: Any): self.store = store + + async def correct(self, *, memory_id: str, correction_event_id: str, replacement: MemoryRecord) -> dict[str, Any]: + return await self.store._run_recoverable_database_operation(self._correct_sync, memory_id, correction_event_id, replacement) + + def _correct_sync(self, memory_id: str, correction_event_id: str, replacement: MemoryRecord) -> dict[str, Any]: + memory_id, correction_event_id = clean_text(memory_id, 120), clean_text(correction_event_id, 160) + if not memory_id or not correction_event_id: return {"ok": False, "error_code": "invalid_correction"} + with self.store._lock: + with self.store._transaction_sync(): + original = self.store._conn.execute("SELECT * FROM memories WHERE id=?", (memory_id,)).fetchone() + if original is None: return {"ok": False, "error_code": "memory_not_found"} + prior = self.store._conn.execute("SELECT id FROM memories WHERE correction_event_id=?", (correction_event_id,)).fetchone() + if prior: return {"ok": True, "record_id": prior['id'], "deduplicated": True} + active_successor = self.store._conn.execute( + "SELECT 1 FROM memories WHERE supersedes_id=? AND fact_status='confirmed' LIMIT 1", + (memory_id,), + ).fetchone() + if original['fact_status'] not in {"confirmed", "legacy"} or active_successor is not None: + return {"ok": False, "error_code": "correction_target_not_current"} + replacement.ensure_defaults() + replacement.id = replacement.id or f"mem_correction_{uuid.uuid4().hex[:16]}" + replacement.supersedes_id = memory_id + replacement.person_id = original['person_id'] + replacement.observed_person_id = original['observed_person_id'] + replacement.origin_identity_key = original['origin_identity_key'] + replacement.origin_scope = original['origin_scope'] + replacement.correction_event_id = correction_event_id + replacement.fact_status = "confirmed" + normalize_record(replacement) + data = replacement.to_db() + columns = ', '.join(data); placeholders = ', '.join(':' + key for key in data) + self.store._conn.execute(f"INSERT INTO memories ({columns}) VALUES ({placeholders})", data) + self.store._conn.execute("INSERT INTO person_memory_correction_backup VALUES(?,?,?,?,?)", (correction_event_id, memory_id, replacement.id, original['fact_status'], utc_now())) + self._backup_artifacts(correction_event_id, memory_id) + self.store._conn.execute("UPDATE memories SET fact_status='superseded', updated_at=? WHERE id=?", (utc_now(), memory_id)) + self.store._delete_memory_fts_row(memory_id) + self.store._conn.execute("DELETE FROM memory_embeddings WHERE memory_id=?", (memory_id,)) + self.store._conn.execute("DELETE FROM relationship_edges WHERE source_memory_id=?", (memory_id,)) + self.store._conn.execute("DELETE FROM knowledge_edges WHERE source_memory_id=?", (memory_id,)) + self.store._conn.execute("DELETE FROM review_queue WHERE memory_id=?", (memory_id,)) + self.store._conn.execute("DELETE FROM unified_person_coordination_queue WHERE memory_id=?", (memory_id,)) + candidates = self.store._conn.execute("SELECT id, metadata, fact_status, lifecycle FROM memories WHERE id NOT IN (?, ?)", (memory_id, replacement.id)).fetchall() + for derived_row in candidates: + derived_id = derived_row['id'] + metadata = json_loads(derived_row['metadata'], {}) + if not self._references_source_memory(metadata, memory_id): + continue + self._backup_artifacts(correction_event_id, derived_id) + self.store._conn.execute("INSERT OR IGNORE INTO person_memory_correction_derived_backup VALUES(?,?,?,?)", (correction_event_id, derived_id, derived_row['fact_status'], derived_row['lifecycle'])) + self.store._conn.execute("UPDATE memories SET fact_status='superseded', lifecycle='archived', updated_at=? WHERE id=?", (utc_now(), derived_id)) + self.store._delete_memory_fts_row(derived_id) + self.store._conn.execute("DELETE FROM memory_embeddings WHERE memory_id=?", (derived_id,)) + self.store._conn.execute("UPDATE memories SET fact_status='superseded', updated_at=? WHERE supersedes_id=? AND id<>?", (utc_now(), memory_id, replacement.id)) + self.store._upsert_memory_fts_row(self.store._conn.execute("SELECT * FROM memories WHERE id=?", (replacement.id,)).fetchone()) + return {"ok": True, "record_id": replacement.id, "deduplicated": False} + + def _backup_artifacts(self, correction_event_id: str, memory_id: str) -> None: + for table, column in (("memory_embeddings", "memory_id"), ("relationship_edges", "source_memory_id"), ("knowledge_edges", "source_memory_id"), ("review_queue", "memory_id"), ("unified_person_coordination_queue", "memory_id")): + for row in self.store._conn.execute(f"SELECT * FROM {table} WHERE {column}=?", (memory_id,)).fetchall(): + self.store._conn.execute("INSERT OR IGNORE INTO person_memory_correction_artifact_backup VALUES(?,?,?)", (correction_event_id, table, json_dumps(dict(row)))) + + @staticmethod + def _references_source_memory(metadata: Any, memory_id: str) -> bool: + if not isinstance(metadata, dict): return False + single = clean_text(metadata.get("source_memory_id"), 120) + if single == memory_id: return True + values = metadata.get("source_memory_ids") + return isinstance(values, list) and memory_id in {clean_text(item, 120) for item in values} + + async def rollback_correction(self, correction_event_id: str) -> dict[str, Any]: + return await self.store._run_recoverable_database_operation(self._rollback_correction_sync, correction_event_id) + + def _rollback_correction_sync(self, correction_event_id: str) -> dict[str, Any]: + with self.store._lock: + with self.store._transaction_sync(): + row = self.store._conn.execute("SELECT * FROM person_memory_correction_backup WHERE correction_event_id=?", (clean_text(correction_event_id, 160),)).fetchone() + if row is None: return {"ok": False, "error_code": "correction_not_found"} + replacement_id = row['replacement_memory_id'] + replacement = self.store._conn.execute("SELECT * FROM memories WHERE id=?", (replacement_id,)).fetchone() + original = self.store._conn.execute("SELECT * FROM memories WHERE id=?", (row['original_memory_id'],)).fetchone() + active_successor = self.store._conn.execute( + "SELECT 1 FROM memories WHERE supersedes_id=? AND fact_status='confirmed' LIMIT 1", + (replacement_id,), + ).fetchone() + if ( + replacement is None + or original is None + or replacement['fact_status'] != "confirmed" + or replacement['firewall_status'] != "allowed" + or replacement['supersedes_id'] != row['original_memory_id'] + or replacement['correction_event_id'] != row['correction_event_id'] + or original['fact_status'] != "superseded" + or active_successor is not None + ): + return {"ok": False, "error_code": "correction_conflict"} + self.store._delete_memory_fts_row(replacement_id) + self.store._conn.execute("DELETE FROM memory_embeddings WHERE memory_id=?", (replacement_id,)) + self.store._conn.execute("DELETE FROM review_queue WHERE memory_id=?", (replacement_id,)) + self.store._conn.execute("DELETE FROM unified_person_coordination_queue WHERE memory_id=?", (replacement_id,)) + self.store._conn.execute("DELETE FROM relationship_edges WHERE source_memory_id=?", (replacement_id,)) + self.store._conn.execute("DELETE FROM knowledge_edges WHERE source_memory_id=?", (replacement_id,)) + self.store._conn.execute("DELETE FROM memories WHERE id=?", (row['replacement_memory_id'],)) + self.store._conn.execute("UPDATE memories SET fact_status=?, updated_at=? WHERE id=?", (row['original_fact_status'], utc_now(), row['original_memory_id'])) + restored_original = self.store._conn.execute("SELECT * FROM memories WHERE id=?", (row['original_memory_id'],)).fetchone() + self.store._upsert_memory_fts_row(restored_original) + derived = self.store._conn.execute("SELECT * FROM person_memory_correction_derived_backup WHERE correction_event_id=?", (row['correction_event_id'],)).fetchall() + for item in derived: + self.store._conn.execute("UPDATE memories SET fact_status=?, lifecycle=?, updated_at=? WHERE id=?", (item['fact_status'], item['lifecycle'], utc_now(), item['memory_id'])) + self.store._upsert_memory_fts_row(self.store._conn.execute("SELECT * FROM memories WHERE id=?", (item['memory_id'],)).fetchone()) + self.store._conn.execute("DELETE FROM person_memory_correction_derived_backup WHERE correction_event_id=?", (row['correction_event_id'],)) + artifacts = self.store._conn.execute("SELECT artifact_type, payload FROM person_memory_correction_artifact_backup WHERE correction_event_id=?", (row['correction_event_id'],)).fetchall() + allowed_artifact_tables = { + "memory_embeddings", "relationship_edges", "knowledge_edges", + "review_queue", "unified_person_coordination_queue", + } + for artifact in artifacts: + if artifact['artifact_type'] not in allowed_artifact_tables: + continue + payload = json_loads(artifact['payload'], {}) + if not isinstance(payload, dict) or not payload: continue + columns = ', '.join(payload); values = ', '.join(':' + key for key in payload) + self.store._conn.execute(f"INSERT OR REPLACE INTO {artifact['artifact_type']} ({columns}) VALUES ({values})", payload) + self.store._conn.execute("DELETE FROM person_memory_correction_artifact_backup WHERE correction_event_id=?", (row['correction_event_id'],)) + self.store._conn.execute("DELETE FROM person_memory_correction_backup WHERE correction_event_id=?", (row['correction_event_id'],)) + return {"ok": True, "restored_memory_id": row['original_memory_id']} diff --git a/core/person_memory_contract.py b/core/person_memory_contract.py new file mode 100644 index 0000000..25a1052 --- /dev/null +++ b/core/person_memory_contract.py @@ -0,0 +1,89 @@ +"""Versioned P2 envelope for person-memory events. + +P0 projections remain opaque read-only payloads here. This module deliberately +does not import or extend their descriptor, so a P2 rollout cannot drift P0. +""" +from __future__ import annotations + +import hashlib +import json +from collections.abc import Mapping +from typing import Any + +from .models import clean_text +from .unified_person_contract import consume_unified_person_projection + + +CONTRACT_NAME = "ops.person_memory.v2" +CONTRACT_VERSION = "2.0" +EVENT_KINDS = frozenset({"fact", "correction", "firewall"}) +FACT_STATUSES = frozenset({"pending", "confirmed", "superseded", "rejected", "quarantined", "legacy"}) +FIREWALL_STATUSES = frozenset({"allowed", "blocked", "rejected", "quarantined"}) +ORIGIN_SCOPES = frozenset({"private", "group", "shared", "legacy"}) + + +def _canonical_json(value: Any) -> str: + return json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":")) + + +def contract_descriptor() -> dict[str, Any]: + return { + "name": CONTRACT_NAME, + "version": CONTRACT_VERSION, + "event_kinds": sorted(EVENT_KINDS), + "fact_statuses": sorted(FACT_STATUSES), + "firewall_statuses": sorted(FIREWALL_STATUSES), + "origin_scopes": sorted(ORIGIN_SCOPES), + "p0_projection": "ops.unified_person.v1/1.0 read-only nested payload", + } + + +def contract_fingerprint() -> str: + return hashlib.sha256(_canonical_json(contract_descriptor()).encode("utf-8")).hexdigest() + + +def capabilities(*, available: bool = True, warnings: list[str] | None = None) -> dict[str, Any]: + return { + "available": bool(available), "contract_name": CONTRACT_NAME, + "contract_version": CONTRACT_VERSION, "contract_fingerprint": contract_fingerprint(), + "event_kinds": sorted(EVENT_KINDS), "warnings": list(warnings or []), + } + + +def consume_person_memory_envelope( + envelope: Mapping[str, Any] | None, *, companion_available: bool = True, +) -> dict[str, Any]: + """Validate P2 metadata and, if supplied, validate the nested frozen P0 projection.""" + if not companion_available: + return {"accepted": False, "status": "quarantined", "error_code": "bridge_unavailable", "envelope": None} + if not isinstance(envelope, Mapping): + return {"accepted": False, "status": "quarantined", "error_code": "invalid_envelope", "envelope": None} + payload = dict(envelope) + if payload.get("contract_name") != CONTRACT_NAME or payload.get("contract_version") != CONTRACT_VERSION: + return {"accepted": False, "status": "quarantined", "error_code": "contract_mismatch", "envelope": None} + if payload.get("contract_fingerprint") != contract_fingerprint(): + return {"accepted": False, "status": "quarantined", "error_code": "fingerprint_mismatch", "envelope": None} + event_kind = clean_text(payload.get("event_kind"), 40) + fact_status = clean_text(payload.get("fact_status"), 40) + firewall_status = clean_text(payload.get("firewall_status"), 40) + origin_scope = clean_text(payload.get("origin_scope"), 40) + event_id = clean_text(payload.get("event_id"), 160) + if not event_id or event_kind not in EVENT_KINDS or fact_status not in FACT_STATUSES or firewall_status not in FIREWALL_STATUSES or origin_scope not in ORIGIN_SCOPES: + return {"accepted": False, "status": "quarantined", "error_code": "invalid_field", "envelope": None} + projection = payload.get("p0_projection") + source = payload.get("source_identity") + if projection is not None: + nested = consume_unified_person_projection(projection, source_identity=source, companion_available=True) + if not nested.get("accepted"): + return {"accepted": False, "status": "quarantined", "error_code": "invalid_p0_projection", "envelope": None} + readonly = { + "contract_name": CONTRACT_NAME, "contract_version": CONTRACT_VERSION, + "contract_fingerprint": contract_fingerprint(), "event_id": event_id, + "event_kind": event_kind, "fact_status": fact_status, "firewall_status": firewall_status, + "origin_scope": origin_scope, "correction_event_id": clean_text(payload.get("correction_event_id"), 160), + "projection_event_id": clean_text(payload.get("projection_event_id"), 160), + "firewall_event_id": clean_text(payload.get("firewall_event_id"), 160), + "source_identity": dict(source) if isinstance(source, Mapping) else None, + "p0_projection": projection, + } + return {"accepted": True, "status": "resolved", "error_code": "", "envelope": readonly} diff --git a/core/retrieval.py b/core/retrieval.py index b82205c..70ebcf0 100644 --- a/core/retrieval.py +++ b/core/retrieval.py @@ -10,6 +10,7 @@ from typing import Any from .identity import session_target_id from .models import MemoryRecord, SearchResult, SessionContext, clean_text +from .person_memory import visibility_reason as person_memory_visibility_reason from .store import MemoryStore from .time_intent import TimeIntent from .visibility import VisibilityPolicy @@ -1621,6 +1622,9 @@ class RetrievalEngine: ctx: SessionContext, acl_state: dict[str, object], ) -> tuple[str, str]: + person_reason = person_memory_visibility_reason(memory, ctx) + if person_reason: + return "", person_reason visible, visibility_reason = self.policy.is_visible(memory, ctx) acl_deny_reason = self._acl_deny_reason(memory, ctx, acl_state) if visible: diff --git a/core/service.py b/core/service.py index 74f68e8..0801e3c 100644 --- a/core/service.py +++ b/core/service.py @@ -85,6 +85,8 @@ from .time_intent import TimeIntent, parse_time_intent from .turn_signal import analyze_turn_signal, message_terms from .user_memory_boundary import UserMemoryBoundaryError, validate_user_memory_record from .user_memory_migration import UserMemoryMigration +from .unified_person_persistence import UnifiedPersonPersistence, UnifiedPersonReferenceMigration +from .person_memory import PersonMemoryFacts, PersonMemoryMigration from .visibility import VisibilityPolicy @@ -192,6 +194,10 @@ class MemoryCompanionService: self._RETRIEVAL_RESULT_CACHE_TTL: float = 45.0 self._RETRIEVAL_RESULT_CACHE_MAX: int = 128 self.user_memory_migration = UserMemoryMigration(self.store) + self.unified_person_persistence = UnifiedPersonPersistence(self.store) + self.unified_person_reference_migration = UnifiedPersonReferenceMigration(self.store) + self.person_memory_migration = PersonMemoryMigration(self.store) + self.person_memory_facts = PersonMemoryFacts(self.store) self.portable_archive = PortableMemoryArchive(self.store, self.data_dir) self.chat_importer = HistoricalChatImporter(self) self.qq_history_reader = QQHistoryReader(self) @@ -215,6 +221,71 @@ class MemoryCompanionService: async def rollback_user_memory_migration(self, migration_id: str) -> dict[str, Any]: return await self.user_memory_migration.rollback(migration_id) + async def bridge_coordinate_unified_person_reference( + self, + *, + memory_id: str, + projection: dict[str, Any] | None, + source_identity: dict[str, Any] | None, + companion_available: bool = True, + operation_id: str = "", + ) -> dict[str, Any]: + return await self.unified_person_persistence.coordinate( + memory_id=memory_id, + projection=projection, + source_identity=source_identity, + companion_available=companion_available, + operation_id=operation_id, + ) + + async def bridge_claim_unified_person_coordination(self, *, limit: int = 20) -> list[dict[str, Any]]: + return await self.unified_person_persistence.claim_pending(limit=limit) + + async def bridge_replay_unified_person_coordination( + self, + *, + queue_id: str, + lease_token: str, + projection: dict[str, Any] | None, + source_identity: dict[str, Any] | None, + companion_available: bool = True, + ) -> dict[str, Any]: + return await self.unified_person_persistence.replay_claim( + queue_id=queue_id, + lease_token=lease_token, + projection=projection, + source_identity=source_identity, + companion_available=companion_available, + ) + + async def preview_unified_person_reference_migration(self, *, limit: int = 5000) -> dict[str, Any]: + return await self.unified_person_reference_migration.preview(limit=limit) + + async def apply_unified_person_reference_migration(self, *, migration_id: str, limit: int = 5000) -> dict[str, Any]: + return await self.unified_person_reference_migration.apply(migration_id=migration_id, limit=limit) + + async def rollback_unified_person_reference_migration(self, migration_id: str) -> dict[str, Any]: + return await self.unified_person_reference_migration.rollback(migration_id) + + async def preview_person_memory_migration(self, *, limit: int = 5000) -> dict[str, Any]: + return await self.person_memory_migration.preview(limit=limit) + + async def apply_person_memory_migration(self, *, migration_id: str = "", limit: int = 5000) -> dict[str, Any]: + return await self.person_memory_migration.apply(migration_id=migration_id, limit=limit) + + async def rollback_person_memory_migration(self, migration_id: str) -> dict[str, Any]: + return await self.person_memory_migration.rollback(migration_id) + + async def bridge_correct_person_memory( + self, *, memory_id: str, correction_event_id: str, replacement: MemoryRecord, + ) -> dict[str, Any]: + return await self.person_memory_facts.correct( + memory_id=memory_id, correction_event_id=correction_event_id, replacement=replacement, + ) + + async def bridge_rollback_person_memory_correction(self, correction_event_id: str) -> dict[str, Any]: + return await self.person_memory_facts.rollback_correction(correction_event_id) + def preview_historical_chat_upload( self, *, @@ -289,7 +360,8 @@ class MemoryCompanionService: reply_chain = await self._reply_chain_for_event(event) await self._apply_user_reaction_feedback(ctx) - self._update_address_evolution(ctx, ctx.message_text or "") + # P2: relationship phase/address history is legacy read-only evidence; + # this memory plugin no longer advances relationship authority. try: await self.inject_memories(ctx, req, event=event) @@ -1635,7 +1707,7 @@ class MemoryCompanionService: companion_bot_mood=companion_bot_mood, companion_bot_energy=companion_bot_energy, time_of_day="" if outfit_focus else self._compute_time_of_day(), - address_hint="" if outfit_focus else self._address_hint_for_injection(ctx), + address_hint="" if outfit_focus else self._address_hint_for_injection(ctx, legacy_compat=False), ) logger.info( "[MemoryCompanion] %s快速上下文已生成: session=%s candidates=%s selected=%s chars=%s elapsed_ms=%s", @@ -2900,6 +2972,8 @@ class MemoryCompanionService: user_name=record.subject.name if record.subject.kind == "user" else "", group_id=record.group_id, message_id=record.message_id, + person_id=record.person_id, + person_origin_identity_key=record.origin_identity_key, ) def _snapshot_context(self, ctx: SessionContext) -> SessionContext: @@ -2917,6 +2991,9 @@ class MemoryCompanionService: strict_session_only=ctx.strict_session_only, preferred_address=ctx.preferred_address, preferred_address_locked=ctx.preferred_address_locked, + person_id=ctx.person_id, + person_origin_identity_key=ctx.person_origin_identity_key, + person_identity_assurance=ctx.person_identity_assurance, ) def _bot_entity(self, ctx: SessionContext | None = None) -> EntityRef: @@ -4752,7 +4829,7 @@ class MemoryCompanionService: companion_bot_energy=merged_bot_energy, time_of_day=self._compute_time_of_day(), cross_window_emotional_hint=self._get_cross_window_emotional_hint(ctx), - address_hint=self._address_hint_for_injection(ctx), + address_hint=self._address_hint_for_injection(ctx, legacy_compat=False), recent_fact_context=recent_fact_context, recent_cross_window_context=recent_cross_window_context, ) @@ -4766,7 +4843,7 @@ class MemoryCompanionService: companion_bot_energy=merged_bot_energy, emotional_tone=getattr(turn_signal, "emotional_tone", "neutral"), ) - self._maybe_record_persona_touch(ctx, results, emotional_tone=getattr(turn_signal, "emotional_tone", "neutral")) + # P2 retires memory-owned relationship phase advancement. self._log_injection_debug( ctx=ctx, intent=intent, @@ -5038,7 +5115,7 @@ class MemoryCompanionService: companion_bot_energy=_bot_energy, time_of_day=self._compute_time_of_day(), cross_window_emotional_hint=self._get_cross_window_emotional_hint(ctx), - address_hint=self._address_hint_for_injection(ctx), + address_hint=self._address_hint_for_injection(ctx, legacy_compat=False), recent_fact_context=recent_fact_context, recent_cross_window_context=recent_cross_window_context, ) @@ -5052,7 +5129,7 @@ class MemoryCompanionService: companion_bot_energy=_bot_energy, emotional_tone=getattr(turn_signal, "emotional_tone", "neutral"), ) - self._maybe_record_persona_touch(ctx, results, emotional_tone=getattr(turn_signal, "emotional_tone", "neutral")) + # P2 retires memory-owned relationship phase advancement. self._log_injection_debug( ctx=ctx, intent=intent, @@ -7709,7 +7786,7 @@ class MemoryCompanionService: user_id=str(payload.get("user_id") or ""), group_id=str(payload.get("group_id") or ""), ) - return SessionContext( + ctx = SessionContext( session_id=normalized["session_id"], scope=normalized["scope"], platform=normalized["platform"], @@ -7724,6 +7801,15 @@ class MemoryCompanionService: preferred_address=str(payload.get("preferred_address") or ""), preferred_address_locked=bool(payload.get("preferred_address_locked", False)), ) + from .unified_person_contract import consume_unified_person_projection + resolved = consume_unified_person_projection( + payload.get("unified_person_projection"), source_identity=payload.get("unified_person_source_identity"), + ) if payload.get("unified_person_projection") is not None else {} + if resolved.get("accepted"): + ctx.person_id = clean_text(resolved.get("person_id"), 160) + ctx.person_origin_identity_key = clean_text(resolved.get("origin_identity_key"), 160) + ctx.person_identity_assurance = clean_text(resolved.get("projection", {}).get("identity_assurance"), 40) + return ctx def _normalized_session_context(self, ctx: SessionContext) -> SessionContext: normalized = normalize_session_context_fields( @@ -7747,6 +7833,9 @@ class MemoryCompanionService: strict_session_only=ctx.strict_session_only, preferred_address=ctx.preferred_address, preferred_address_locked=ctx.preferred_address_locked, + person_id=ctx.person_id, + person_origin_identity_key=ctx.person_origin_identity_key, + person_identity_assurance=ctx.person_identity_assurance, ) def stable_id(self, *parts: Any) -> str: @@ -8029,7 +8118,7 @@ class MemoryCompanionService: state["current_address_phase"] = phase self._save_relationship_phase_state() - def _address_hint_for_injection(self, ctx: SessionContext) -> str: + def _address_hint_for_injection(self, ctx: SessionContext, *, legacy_compat: bool = True) -> str: """Generate address hint for injection context. Combines user-side address phase detection with bot-side address suggestions @@ -8043,6 +8132,8 @@ class MemoryCompanionService: "不必每句都带称呼;关系阶段建议、历史称呼、显示名和旧记忆只作识别资料," "不能另造或追加亲昵称呼。若对方本轮明确要求改称呼,以本轮最新要求为准。" ) + if not legacy_compat: + return "" state = self._get_relationship_phase(ctx) user_address_phase = state.get("current_address_phase", "") relationship_phase = state.get("phase", "acquaintance") diff --git a/core/store.py b/core/store.py index 7a014fa..af595eb 100644 --- a/core/store.py +++ b/core/store.py @@ -11,6 +11,9 @@ from typing import Any from .identity import parse_scope_from_session from .models import EntityRef, MemoryRecord, clean_text, json_dumps, json_loads, new_id, stable_fingerprint, utc_now +from .unified_person_persistence import ensure_unified_person_persistence_schema +from .person_memory import apply_envelope, ensure_person_memory_schema, indexable, normalize_record +from .person_memory_contract import consume_person_memory_envelope class MemoryStore: @@ -490,6 +493,8 @@ class MemoryStore: """ ) self._ensure_memory_columns_sync() + ensure_unified_person_persistence_schema(self._conn) + ensure_person_memory_schema(self._conn) self._conn.execute( "CREATE INDEX IF NOT EXISTS idx_memories_domain ON memories(memory_domain, lifecycle, scope, group_id, subject_id)" ) @@ -698,8 +703,12 @@ class MemoryStore: memory_id = clean_text(row["id"], 120) if not memory_id: return - search_text = self._memory_fts_text(row) self._conn.execute("DELETE FROM memory_fts WHERE memory_id=?", (memory_id,)) + # P2 firewall: blocked, rejected, quarantined and superseded facts are + # never materialized into the ordinary text index. + if clean_text(row["fact_status"], 40) in {"superseded", "rejected", "quarantined"} or clean_text(row["firewall_status"], 40) in {"blocked", "rejected", "quarantined"}: + return + search_text = self._memory_fts_text(row) if search_text: self._conn.execute( "INSERT INTO memory_fts(memory_id, search_text) VALUES(?, ?)", @@ -1028,6 +1037,7 @@ class MemoryStore: with self._transaction_sync(): if memory_ids: self._delete_many_by_ids("review_queue", "memory_id", memory_ids, deleted) + self._delete_many_by_ids("unified_person_coordination_queue", "memory_id", memory_ids, deleted) self._delete_many_by_ids("memory_embeddings", "memory_id", memory_ids, deleted) self._delete_many_by_ids("knowledge_edges", "source_memory_id", memory_ids, deleted) self._delete_many_by_ids("relationship_edges", "source_memory_id", memory_ids, deleted) @@ -1219,16 +1229,45 @@ class MemoryStore: ) def _insert_memory_sync(self, record: MemoryRecord, review_reason: str = "") -> str: - record.ensure_defaults() - data = record.to_db() - columns = ", ".join(data.keys()) - placeholders = ", ".join(f":{key}" for key in data.keys()) - updates = ", ".join( - f"{key}=excluded.{key}" - for key in data.keys() - if key not in {"id", "created_at"} - ) + with self._lock, self._transaction_sync(): + return self._insert_memory_locked_sync(record, review_reason) + + def _insert_memory_locked_sync(self, record: MemoryRecord, review_reason: str = "") -> str: + # Replayed P2 envelopes may arrive as a fresh DTO. Preserve immutable + # source evidence from the persisted row before applying its current + # companion projection. with self._lock: + existing_row = self._conn.execute("SELECT * FROM memories WHERE id=?", (record.id,)).fetchone() if record.id else None + if existing_row is not None: + existing_record = MemoryRecord.from_row(existing_row) + if existing_record.observed_person_id: + record.observed_person_id = existing_record.observed_person_id + if existing_record.origin_identity_key: + record.origin_identity_key = existing_record.origin_identity_key + if existing_record.evidence: + record.evidence = existing_record.evidence + apply_envelope(record) + normalize_record(record) + record.ensure_defaults() + data = record.to_db() + columns = ", ".join(data.keys()) + placeholders = ", ".join(f":{key}" for key in data.keys()) + updates = ", ".join( + f"{key}=excluded.{key}" + for key in data.keys() + if key not in {"id", "created_at"} + ) + with self._lock: + # Re-check during the write transaction: P2 fact envelopes are + # create/idempotency only and cannot reproject a stored record. + persisted = self._conn.execute("SELECT * FROM memories WHERE id=?", (record.id,)).fetchone() if record.id else None + envelope = record.metadata.get("person_memory_envelope") if isinstance(record.metadata, dict) else None + if persisted is not None and envelope is not None: + stored = MemoryRecord.from_row(persisted) + # P2 fact envelope is create/idempotency only. Current-person + # re-projection (merge/split) belongs exclusively to P1's + # revisioned UnifiedPersonPersistence transaction. + return stored.id duplicate = None if record.content_fingerprint: duplicate = self._conn.execute( @@ -1327,7 +1366,6 @@ class MemoryStore: ) row = self._conn.execute("SELECT * FROM memories WHERE id=?", (duplicate["id"],)).fetchone() self._upsert_memory_fts_row(row) - self._conn.commit() return str(duplicate["id"]) self._conn.execute( f"INSERT INTO memories ({columns}) VALUES ({placeholders}) " @@ -1338,7 +1376,6 @@ class MemoryStore: self._upsert_review_sync(record.id, review_reason or "待人工确认") row = self._conn.execute("SELECT * FROM memories WHERE id=?", (record.id,)).fetchone() self._upsert_memory_fts_row(row) - self._conn.commit() return record.id async def upsert_bot_personal_memory( @@ -1576,6 +1613,10 @@ class MemoryStore: source_memory_id: str, metadata: dict[str, Any], ) -> str: + if source_memory_id: + source = self._conn.execute("SELECT fact_status, firewall_status FROM memories WHERE id=?", (clean_text(source_memory_id, 120),)).fetchone() + if source is None or clean_text(source["fact_status"], 40) in {"superseded", "rejected", "quarantined"} or clean_text(source["firewall_status"], 40) in {"blocked", "rejected", "quarantined"}: + return "" now = utc_now() row_id = new_id("rel") with self._lock: @@ -2146,6 +2187,22 @@ class MemoryStore: break return labels[:limit] + @staticmethod + def _timeline_person_memory_allowed(metadata: dict[str, Any]) -> tuple[bool, dict[str, Any]]: + normalized = dict(metadata) + envelope = normalized.get("person_memory_envelope") + if envelope is None: + status = clean_text(normalized.get("firewall_status"), 40) + return status not in {"blocked", "rejected", "quarantined"}, normalized + result = consume_person_memory_envelope(envelope) + if not result.get("accepted"): + return False, normalized + accepted = result.get("envelope") or {} + firewall_status = clean_text(accepted.get("firewall_status"), 40) + normalized["firewall_status"] = firewall_status + normalized["person_memory_event_id"] = clean_text(accepted.get("event_id"), 160) + return firewall_status not in {"blocked", "rejected", "quarantined"}, normalized + async def add_timeline_event( self, *, @@ -2181,6 +2238,9 @@ class MemoryStore: metadata: dict[str, Any], occurred_at: str, ) -> str: + allowed, metadata = self._timeline_person_memory_allowed(metadata) + if not allowed: + return "" now = utc_now() row_id = new_id("tl") event_type = clean_text(event_type, 80) @@ -2251,6 +2311,9 @@ class MemoryStore: dedupe_key = stable_fingerprint("timeline", event_type, session_id, subject_id, message_id) row_id = clean_text(raw.get("id"), 120) or new_id("tl") metadata = raw.get("metadata") if isinstance(raw.get("metadata"), dict) else {} + allowed, metadata = self._timeline_person_memory_allowed(metadata) + if not allowed: + continue self._conn.execute( """ INSERT OR IGNORE INTO timeline( @@ -2864,6 +2927,7 @@ class MemoryStore: memory_ids = [clean_text(row["id"], 120) for row in memory_rows] for memory_id in memory_ids: self._conn.execute("DELETE FROM review_queue WHERE memory_id=?", (memory_id,)) + self._conn.execute("DELETE FROM unified_person_coordination_queue WHERE memory_id=?", (memory_id,)) self._conn.execute("DELETE FROM memory_embeddings WHERE memory_id=?", (memory_id,)) self._conn.execute("DELETE FROM relationship_edges WHERE source_memory_id=?", (memory_id,)) self._conn.execute("DELETE FROM knowledge_edges WHERE source_memory_id=?", (memory_id,)) @@ -4753,6 +4817,7 @@ class MemoryStore: with self._lock: with self._transaction_sync(): self._conn.execute("DELETE FROM review_queue WHERE memory_id=?", (memory_id,)) + self._conn.execute("DELETE FROM unified_person_coordination_queue WHERE memory_id=?", (memory_id,)) self._conn.execute("DELETE FROM memory_embeddings WHERE memory_id=?", (memory_id,)) self._conn.execute("DELETE FROM relationship_edges WHERE source_memory_id=?", (memory_id,)) self._conn.execute("DELETE FROM knowledge_edges WHERE source_memory_id=?", (memory_id,)) @@ -5150,6 +5215,11 @@ class MemoryStore: return now = utc_now() with self._lock: + row = self._conn.execute("SELECT * FROM memories WHERE id=?", (memory_id,)).fetchone() + if row is None or not indexable(MemoryRecord.from_row(row)): + self._conn.execute("DELETE FROM memory_embeddings WHERE memory_id=?", (memory_id,)) + self._conn.commit() + return self._conn.execute( """ INSERT INTO memory_embeddings( diff --git a/core/unified_person_contract.py b/core/unified_person_contract.py new file mode 100644 index 0000000..26fa8e6 --- /dev/null +++ b/core/unified_person_contract.py @@ -0,0 +1,290 @@ +from __future__ import annotations + +import hashlib +import json +import re +import unicodedata +from collections.abc import Mapping +from typing import Any + +from .models import clean_text + + +CONTRACT_NAME = "ops.unified_person.v1" +CONTRACT_VERSION = "1.0" +SAMPLE_VERSION = "1.0" + +IDENTITY_KEY_FIELDS = ( + "companion_instance_id", + "bot_account_id", + "adapter_instance_id", + "subject_namespace", + "platform_subject_id", +) + +REQUIRED_PROJECTION_FIELDS = ( + "contract_name", + "contract_version", + "contract_fingerprint", + "projection_revision", + "person_id", + "resolved_identity_key", + "identity_assurance", + "profile_status", + "display_name", + "aliases", + "relation_policy_id", + "relation_label", + "relationship_capabilities", + "owner_mode", + "affinity_score", + "affinity_band", + "warning_stage", + "confinement_state", + "confinement_until", + "release_prompt_pending", + "proactive_private_enabled", + "group_overlay_ref", + "updated_at", +) + +ENUM_VALUES = { + "identity_assurance": ("unverified", "observed", "verified", "explicit_linked"), + "profile_status": ("active", "suspended", "quarantined", "deleted"), + "owner_mode": ("none", "owner", "not_owner"), + "affinity_band": ("critical", "guarded", "neutral", "positive"), + "warning_stage": ("none", "warning_1", "warning_2", "confinement"), + "confinement_state": ("none", "active", "released"), + "relationship_capabilities": ( + "comfort", + "proactive_care", + "joking", + "hugging", + "hand_holding", + "head_pat", + "leaning_close", + "sweet_talk", + "affectionate_address", + "missing_you", + "acting_cute", + "jealousy", + "flirting", + "romantic_expression", + "confession", + "exclusivity", + "public_favoritism", + "nsfw", + ), + "coordination_status": ( + "resolved", + "pending_companion", + "contract_mismatch", + "identity_conflict", + "quarantined", + "legacy_unresolved", + ), +} + +RELATIONSHIP_CAPABILITY_VALUES = frozenset(ENUM_VALUES["relationship_capabilities"]) +_ORIGIN_IDENTITY_KEY_RE = re.compile(r"ops-origin-v1:[0-9a-f]{64}\Z") + + +def _canonical_json(value: Any) -> str: + return json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":")) + + +def contract_descriptor() -> dict[str, Any]: + """Return the frozen, fingerprinted parts of the shared contract only.""" + return { + "name": CONTRACT_NAME, + "version": CONTRACT_VERSION, + "sample_version": SAMPLE_VERSION, + "identity_key_fields": sorted(IDENTITY_KEY_FIELDS), + "required_fields": sorted(REQUIRED_PROJECTION_FIELDS), + "enums": {key: sorted(values) for key, values in sorted(ENUM_VALUES.items())}, + } + + +def contract_fingerprint() -> str: + return hashlib.sha256(_canonical_json(contract_descriptor()).encode("utf-8")).hexdigest() + + +def contract_capabilities(*, available: bool = True, warnings: list[str] | None = None) -> dict[str, Any]: + return { + "available": bool(available), + "contract_name": CONTRACT_NAME, + "contract_version": CONTRACT_VERSION, + "sample_version": SAMPLE_VERSION, + "contract_fingerprint": contract_fingerprint(), + "coordination_statuses": sorted(ENUM_VALUES["coordination_status"]), + "error_codes": [ + "bridge_unavailable", + "invalid_payload", + "invalid_contract_name", + "unsupported_contract_version", + "missing_field", + "invalid_field", + "unknown_enum", + "fingerprint_mismatch", + "invalid_identity", + "identity_conflict", + ], + "warnings": list(warnings or []), + } + + +def normalize_source_identity(value: Mapping[str, Any] | None) -> dict[str, str]: + """Normalize a complete source identity without inferring from a platform ID.""" + if not isinstance(value, Mapping): + raise ValueError("invalid_identity") + normalized: dict[str, str] = {} + for field in IDENTITY_KEY_FIELDS: + raw = value.get(field) + if isinstance(raw, bool) or not isinstance(raw, (str, int)): + raise ValueError("invalid_identity") + text = unicodedata.normalize("NFC", str(raw)).strip() + if not text or "\x00" in text or len(text) > 160: + raise ValueError("invalid_identity") + normalized[field] = text.lower() if field == "subject_namespace" else text + return normalized + + +def origin_identity_key(value: Mapping[str, Any] | None) -> str: + normalized = normalize_source_identity(value) + digest = hashlib.sha256(_canonical_json(normalized).encode("utf-8")).hexdigest() + return f"ops-origin-v1:{digest}" + + +def _valid_required_text(value: Any, *, limit: int) -> bool: + return isinstance(value, str) and bool(value.strip()) and "\x00" not in value and len(value) <= limit + + +def _result( + *, + accepted: bool, + status: str, + error_code: str = "", + person_id: str = "", + origin_key: str = "", + projection: dict[str, Any] | None = None, +) -> dict[str, Any]: + return { + "accepted": accepted, + "status": status, + "error_code": error_code, + "person_id": person_id if accepted else "", + "origin_identity_key": origin_key, + "projection": projection if accepted else None, + } + + +def pending_companion_result(*, origin_key: str = "", error_code: str = "bridge_unavailable") -> dict[str, Any]: + return _result( + accepted=False, + status="pending_companion", + error_code=error_code, + origin_key=origin_key, + ) + + +def legacy_unresolved_reference(*, legacy_record_id: str, origin_key: str = "") -> dict[str, Any]: + """Describe a legacy candidate without synthesizing a person identifier.""" + return { + **_result( + accepted=False, + status="legacy_unresolved", + error_code="", + origin_key=clean_text(origin_key, 160), + ), + "legacy_record_id": clean_text(legacy_record_id, 120), + } + + +def consume_unified_person_projection( + projection: Mapping[str, Any] | None, + *, + source_identity: Mapping[str, Any] | None = None, + companion_available: bool = True, +) -> dict[str, Any]: + """Validate a companion-owned projection and return a read-only coordination result.""" + origin_key = "" + if source_identity is not None: + try: + origin_key = origin_identity_key(source_identity) + except ValueError: + return _result(accepted=False, status="quarantined", error_code="invalid_identity") + if not companion_available: + return pending_companion_result(origin_key=origin_key) + if not isinstance(projection, Mapping): + return _result(accepted=False, status="quarantined", error_code="invalid_payload", origin_key=origin_key) + + payload = dict(projection) + if payload.get("contract_name") != CONTRACT_NAME: + return _result(accepted=False, status="contract_mismatch", error_code="invalid_contract_name", origin_key=origin_key) + if payload.get("contract_version") != CONTRACT_VERSION: + return _result(accepted=False, status="contract_mismatch", error_code="unsupported_contract_version", origin_key=origin_key) + if payload.get("contract_fingerprint") != contract_fingerprint(): + return _result(accepted=False, status="contract_mismatch", error_code="fingerprint_mismatch", origin_key=origin_key) + + for field in REQUIRED_PROJECTION_FIELDS: + if field not in payload: + return _result(accepted=False, status="quarantined", error_code="missing_field", origin_key=origin_key) + for field, allowed in ENUM_VALUES.items(): + if field in {"coordination_status", "relationship_capabilities"}: + continue + if payload.get(field) not in allowed: + return _result(accepted=False, status="quarantined", error_code="unknown_enum", origin_key=origin_key) + required_text_limits = { + "person_id": 160, + "relation_policy_id": 160, + "display_name": 160, + "relation_label": 160, + "updated_at": 80, + } + if not all(_valid_required_text(payload.get(field), limit=limit) for field, limit in required_text_limits.items()): + return _result(accepted=False, status="quarantined", error_code="invalid_field", origin_key=origin_key) + if not isinstance(payload.get("resolved_identity_key"), str) or not _ORIGIN_IDENTITY_KEY_RE.fullmatch( + payload["resolved_identity_key"] + ): + return _result(accepted=False, status="quarantined", error_code="invalid_field", origin_key=origin_key) + if ( + isinstance(payload.get("projection_revision"), bool) + or not isinstance(payload.get("projection_revision"), int) + or payload["projection_revision"] < 1 + ): + return _result(accepted=False, status="quarantined", error_code="invalid_field", origin_key=origin_key) + if not isinstance(payload.get("aliases"), list) or not all( + _valid_required_text(item, limit=120) for item in payload["aliases"] + ): + return _result(accepted=False, status="quarantined", error_code="invalid_field", origin_key=origin_key) + if not isinstance(payload.get("relationship_capabilities"), list) or not all( + isinstance(item, str) and item in RELATIONSHIP_CAPABILITY_VALUES for item in payload["relationship_capabilities"] + ): + return _result(accepted=False, status="quarantined", error_code="unknown_enum", origin_key=origin_key) + if ( + isinstance(payload.get("affinity_score"), bool) + or not isinstance(payload.get("affinity_score"), int) + or not -1200 <= payload["affinity_score"] <= 1200 + ): + return _result(accepted=False, status="quarantined", error_code="invalid_field", origin_key=origin_key) + score = payload["affinity_score"] + expected_band = "critical" if score <= -800 else "guarded" if score < 0 else "neutral" if score == 0 else "positive" + if payload.get("affinity_band") != expected_band: + return _result(accepted=False, status="quarantined", error_code="invalid_field", origin_key=origin_key) + if not isinstance(payload.get("release_prompt_pending"), bool) or not isinstance( + payload.get("proactive_private_enabled"), bool + ): + return _result(accepted=False, status="quarantined", error_code="invalid_field", origin_key=origin_key) + if not isinstance(payload.get("confinement_until"), str) or not isinstance(payload.get("group_overlay_ref"), str): + return _result(accepted=False, status="quarantined", error_code="invalid_field", origin_key=origin_key) + if origin_key and payload.get("resolved_identity_key") != origin_key: + return _result(accepted=False, status="identity_conflict", error_code="identity_conflict", origin_key=origin_key) + + readonly_projection = {field: payload[field] for field in REQUIRED_PROJECTION_FIELDS} + return _result( + accepted=True, + status="resolved", + person_id=payload["person_id"], + origin_key=origin_key, + projection=readonly_projection, + ) diff --git a/core/unified_person_persistence.py b/core/unified_person_persistence.py new file mode 100644 index 0000000..396901b --- /dev/null +++ b/core/unified_person_persistence.py @@ -0,0 +1,525 @@ +from __future__ import annotations + +import asyncio +import hashlib +import json +import uuid +from collections.abc import Mapping +from datetime import datetime, timezone +from typing import Any + +from .models import clean_text, json_dumps, utc_now +from .unified_person_contract import ( + consume_unified_person_projection, + normalize_source_identity, + origin_identity_key, +) + + +P1_MIGRATION_VERSION = 1 +RETRYABLE_COORDINATION_STATUSES = frozenset({"pending_companion", "contract_mismatch", "identity_conflict"}) + + +def ensure_unified_person_persistence_schema(connection: Any) -> None: + """Install additive P1 storage without rewriting legacy identity evidence.""" + existing = {row["name"] for row in connection.execute("PRAGMA table_info(memories)").fetchall()} + additions = { + "person_id": "TEXT NOT NULL DEFAULT ''", + "origin_identity_key": "TEXT NOT NULL DEFAULT ''", + "coordination_status": "TEXT NOT NULL DEFAULT 'legacy_unresolved'", + "projection_revision": "INTEGER NOT NULL DEFAULT 0", + } + for name, ddl in additions.items(): + if name not in existing: + connection.execute(f"ALTER TABLE memories ADD COLUMN {name} {ddl}") + connection.executescript( + """ + CREATE TABLE IF NOT EXISTS unified_person_source_identities ( + origin_identity_key TEXT PRIMARY KEY, + companion_instance_id TEXT NOT NULL, + bot_account_id TEXT NOT NULL, + adapter_instance_id TEXT NOT NULL, + subject_namespace TEXT NOT NULL, + platform_subject_id TEXT NOT NULL, + created_at TEXT NOT NULL, + updated_at TEXT NOT NULL, + UNIQUE(companion_instance_id, bot_account_id, adapter_instance_id, subject_namespace, platform_subject_id) + ); + CREATE TABLE IF NOT EXISTS unified_person_coordination_queue ( + id TEXT PRIMARY KEY, + idempotency_key TEXT NOT NULL UNIQUE, + memory_id TEXT NOT NULL REFERENCES memories(id) ON DELETE CASCADE, + origin_identity_key TEXT NOT NULL DEFAULT '', + status TEXT NOT NULL, + error_code TEXT NOT NULL DEFAULT '', + projection_json TEXT NOT NULL DEFAULT '{}', + attempts INTEGER NOT NULL DEFAULT 0, + lease_token TEXT NOT NULL DEFAULT '', + lease_until TEXT NOT NULL DEFAULT '', + confirmed_at TEXT NOT NULL DEFAULT '', + created_at TEXT NOT NULL, + updated_at TEXT NOT NULL + ); + CREATE TABLE IF NOT EXISTS unified_person_migration_runs ( + migration_id TEXT PRIMARY KEY, + migration_version INTEGER NOT NULL, + state TEXT NOT NULL, + backup_path TEXT NOT NULL DEFAULT '', + created_at TEXT NOT NULL, + updated_at TEXT NOT NULL + ); + CREATE TABLE IF NOT EXISTS unified_person_migration_backup ( + migration_id TEXT NOT NULL, + memory_id TEXT NOT NULL, + previous_person_id TEXT NOT NULL, + previous_origin_identity_key TEXT NOT NULL, + previous_coordination_status TEXT NOT NULL, + previous_projection_revision INTEGER NOT NULL, + created_at TEXT NOT NULL, + PRIMARY KEY(migration_id, memory_id) + ); + CREATE INDEX IF NOT EXISTS idx_memories_unified_person + ON memories(person_id, origin_identity_key, coordination_status); + CREATE INDEX IF NOT EXISTS idx_unified_person_queue_replay + ON unified_person_coordination_queue(status, lease_until, updated_at); + CREATE INDEX IF NOT EXISTS idx_unified_person_queue_memory + ON unified_person_coordination_queue(memory_id, origin_identity_key); + CREATE TRIGGER IF NOT EXISTS trg_memories_origin_identity_key_immutable + BEFORE UPDATE OF origin_identity_key ON memories + WHEN OLD.origin_identity_key != '' AND NEW.origin_identity_key != OLD.origin_identity_key + BEGIN + SELECT RAISE(ABORT, 'origin_identity_key_immutable'); + END; + """ + ) + + +def _canonical_json(value: Any) -> str: + return json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":")) + + +def _queue_key(*, memory_id: str, origin_key: str, operation_id: str) -> str: + """Make repeated reports of one coordination event address one queue row. + + Status is deliberately absent: an event can move from unavailable to a + contract error (or back) without multiplying durable work items. + """ + payload = ( + {"operation_id": operation_id} + if operation_id + else {"memory_id": memory_id, "origin_identity_key": origin_key} + ) + return hashlib.sha256(_canonical_json(payload).encode("utf-8")).hexdigest() + + +class UnifiedPersonPersistence: + """Memory-side persistence for companion-authoritative person references only.""" + + def __init__(self, store: Any): + self.store = store + + async def coordinate( + self, + *, + memory_id: str, + projection: Mapping[str, Any] | None, + source_identity: Mapping[str, Any] | None, + companion_available: bool = True, + operation_id: str = "", + ) -> dict[str, Any]: + return await self.store._run_recoverable_database_operation( + self._coordinate_sync, + memory_id, + projection, + source_identity, + companion_available, + operation_id, + ) + + def _coordinate_sync( + self, + memory_id: str, + projection: Mapping[str, Any] | None, + source_identity: Mapping[str, Any] | None, + companion_available: bool, + operation_id: str, + queue_idempotency_key: str = "", + ) -> dict[str, Any]: + memory_id = clean_text(memory_id, 120) + if not memory_id or not self.store._conn.execute("SELECT 1 FROM memories WHERE id=?", (memory_id,)).fetchone(): + return {"ok": False, "status": "quarantined", "error_code": "memory_not_found", "person_id": ""} + source_key = "" + try: + source = normalize_source_identity(source_identity) + source_key = origin_identity_key(source) + except ValueError: + source = None + result = consume_unified_person_projection( + projection, + source_identity=source_identity, + companion_available=companion_available, + ) + if result.get("accepted") and source is not None: + applied = self._apply_projection_sync(memory_id, source, result["projection"]) + if applied.get("ok"): + return applied + result = applied + status = clean_text(result.get("status"), 40) or "quarantined" + error_code = clean_text(result.get("error_code"), 80) or "invalid_identity" + return self._enqueue_sync( + memory_id=memory_id, + origin_key=source_key, + status=status, + error_code=error_code, + projection=dict(projection) if isinstance(projection, Mapping) else {}, + operation_id=operation_id, + queue_idempotency_key=queue_idempotency_key, + ) + + def _apply_projection_sync(self, memory_id: str, source: dict[str, str], projection: Mapping[str, Any]) -> dict[str, Any]: + origin_key = origin_identity_key(source) + person_id = str(projection["person_id"]) + revision = int(projection["projection_revision"]) + now = utc_now() + with self.store._lock: + with self.store._transaction_sync(): + row = self.store._conn.execute( + "SELECT origin_identity_key, person_id, projection_revision FROM memories WHERE id=?", (memory_id,) + ).fetchone() + if not row: + return {"ok": False, "status": "quarantined", "error_code": "memory_not_found", "person_id": ""} + current_origin = str(row["origin_identity_key"] or "") + if current_origin and current_origin != origin_key: + return {"ok": False, "status": "identity_conflict", "error_code": "origin_identity_immutable", "person_id": ""} + current_revision = int(row["projection_revision"] or 0) + current_person = str(row["person_id"] or "") + if revision < current_revision: + return {"ok": False, "status": "contract_mismatch", "error_code": "stale_projection", "person_id": ""} + if revision == current_revision and current_person and current_person != person_id: + return {"ok": False, "status": "identity_conflict", "error_code": "projection_revision_conflict", "person_id": ""} + self.store._conn.execute( + """ + INSERT INTO unified_person_source_identities( + origin_identity_key, companion_instance_id, bot_account_id, adapter_instance_id, + subject_namespace, platform_subject_id, created_at, updated_at + ) VALUES(?,?,?,?,?,?,?,?) + ON CONFLICT(origin_identity_key) DO UPDATE SET updated_at=excluded.updated_at + """, + ( + origin_key, + source["companion_instance_id"], source["bot_account_id"], source["adapter_instance_id"], + source["subject_namespace"], source["platform_subject_id"], now, now, + ), + ) + self.store._conn.execute( + """ + UPDATE memories SET person_id=?, origin_identity_key=CASE WHEN origin_identity_key='' THEN ? ELSE origin_identity_key END, + coordination_status='resolved', projection_revision=?, updated_at=? WHERE id=? + """, + (person_id, origin_key, revision, now, memory_id), + ) + self.store._conn.execute( + """ + UPDATE unified_person_coordination_queue + SET status='confirmed', error_code='', lease_token='', lease_until='', confirmed_at=?, updated_at=? + WHERE memory_id=? AND origin_identity_key=? AND status!='confirmed' + """, + (now, now, memory_id, origin_key), + ) + return {"ok": True, "status": "resolved", "error_code": "", "person_id": person_id, "origin_identity_key": origin_key, "deduplicated": revision == current_revision and current_person == person_id} + + def _enqueue_sync( + self, + *, + memory_id: str, + origin_key: str, + status: str, + error_code: str, + projection: Mapping[str, Any], + operation_id: str, + queue_idempotency_key: str = "", + ) -> dict[str, Any]: + operation_id = clean_text(operation_id, 160) + key = clean_text(queue_idempotency_key, 128) or _queue_key( + memory_id=memory_id, + origin_key=origin_key, + operation_id=operation_id, + ) + now = utc_now() + queue_id = f"upq_{key[:24]}" + with self.store._lock: + with self.store._transaction_sync(): + existing = self.store._conn.execute( + """SELECT id, memory_id, origin_identity_key, status + FROM unified_person_coordination_queue WHERE idempotency_key=?""", + (key,), + ).fetchone() + if operation_id and existing and ( + str(existing["memory_id"]) != memory_id + or str(existing["origin_identity_key"]) != origin_key + ): + return { + "ok": False, + "status": "identity_conflict", + "error_code": "operation_id_conflict", + "person_id": "", + "queue_id": "", + "deduplicated": False, + } + self.store._conn.execute( + """ + INSERT INTO unified_person_coordination_queue( + id, idempotency_key, memory_id, origin_identity_key, status, error_code, projection_json, created_at, updated_at + ) VALUES(?,?,?,?,?,?,?,?,?) + ON CONFLICT(idempotency_key) DO UPDATE SET + status=CASE WHEN unified_person_coordination_queue.status='confirmed' THEN 'confirmed' ELSE excluded.status END, + error_code=CASE WHEN unified_person_coordination_queue.status='confirmed' THEN '' ELSE excluded.error_code END, + projection_json=CASE WHEN unified_person_coordination_queue.status='confirmed' THEN unified_person_coordination_queue.projection_json ELSE excluded.projection_json END, + updated_at=excluded.updated_at + WHERE unified_person_coordination_queue.memory_id=excluded.memory_id + AND unified_person_coordination_queue.origin_identity_key=excluded.origin_identity_key + """, + (queue_id, key, memory_id, origin_key, status, error_code, json_dumps(dict(projection)), now, now), + ) + row = self.store._conn.execute( + """SELECT id, memory_id, origin_identity_key, status, attempts + FROM unified_person_coordination_queue WHERE idempotency_key=?""", + (key,), + ).fetchone() + if operation_id and ( + str(row["memory_id"]) != memory_id + or str(row["origin_identity_key"]) != origin_key + ): + return { + "ok": False, + "status": "identity_conflict", + "error_code": "operation_id_conflict", + "person_id": "", + "queue_id": "", + "deduplicated": False, + } + if str(row["status"]) != "confirmed": + self.store._conn.execute( + """ + UPDATE memories SET coordination_status=?, updated_at=? + WHERE id=? + AND NOT (person_id!='' AND coordination_status='resolved') + """, + (status, now, memory_id), + ) + queue_status = str(row["status"]) + return { + "ok": False, + "status": queue_status, + "error_code": "" if queue_status == "confirmed" else error_code, + "person_id": "", + "queue_id": str(row["id"]), + "deduplicated": existing is not None, + } + + async def claim_pending(self, *, limit: int = 20) -> list[dict[str, Any]]: + return await self.store._run_recoverable_database_operation(self._claim_pending_sync, max(1, int(limit or 1))) + + def _claim_pending_sync(self, limit: int) -> list[dict[str, Any]]: + now = utc_now() + lease_until = (datetime.now(timezone.utc).timestamp() + 60) + lease_until_text = datetime.fromtimestamp(lease_until, timezone.utc).isoformat(timespec="seconds") + claimed: list[dict[str, Any]] = [] + with self.store._lock: + with self.store._transaction_sync(): + placeholders = ",".join("?" for _ in RETRYABLE_COORDINATION_STATUSES) + rows = self.store._conn.execute( + f"""SELECT * FROM unified_person_coordination_queue + WHERE status IN ({placeholders}) AND (lease_until='' OR lease_until dict[str, Any]: + return await self.store._run_recoverable_database_operation( + self._replay_claim_sync, queue_id, lease_token, projection, source_identity, companion_available + ) + + def _replay_claim_sync(self, queue_id: str, lease_token: str, projection: Mapping[str, Any] | None, source_identity: Mapping[str, Any] | None, companion_available: bool) -> dict[str, Any]: + now = utc_now() + placeholders = ",".join("?" for _ in RETRYABLE_COORDINATION_STATUSES) + with self.store._lock: + row = self.store._conn.execute( + f"""SELECT * FROM unified_person_coordination_queue + WHERE id=? AND lease_token=? AND lease_until>? AND status IN ({placeholders})""", + (queue_id, lease_token, now, *sorted(RETRYABLE_COORDINATION_STATUSES)), + ).fetchone() + if not row: + return {"ok": False, "status": "quarantined", "error_code": "lease_not_owned", "person_id": ""} + # The store lock keeps a valid lease exclusively owned through the + # synchronous projection application and its terminal queue update. + result = self._coordinate_sync( + str(row["memory_id"]), + projection, + source_identity, + companion_available, + "", + str(row["idempotency_key"]), + ) + now = utc_now() + with self.store._transaction_sync(): + if result.get("ok"): + self.store._conn.execute( + f"""UPDATE unified_person_coordination_queue + SET status='confirmed', error_code='', lease_token='', lease_until='', confirmed_at=?, updated_at=? + WHERE id=? AND lease_token=? AND lease_until>? AND status IN ({placeholders})""", + (now, now, queue_id, lease_token, now, *sorted(RETRYABLE_COORDINATION_STATUSES)), + ) + else: + self.store._conn.execute( + f"""UPDATE unified_person_coordination_queue + SET status=?, error_code=?, lease_token='', lease_until='', updated_at=? + WHERE id=? AND lease_token=? AND lease_until>? AND status IN ({placeholders})""", + ( + result.get("status") or "quarantined", result.get("error_code") or "", now, + queue_id, lease_token, now, *sorted(RETRYABLE_COORDINATION_STATUSES), + ), + ) + return result + + async def queue_rows(self) -> list[dict[str, Any]]: + return await self.store._run_recoverable_database_operation(self._queue_rows_sync) + + def _queue_rows_sync(self) -> list[dict[str, Any]]: + with self.store._lock: + rows = self.store._conn.execute("SELECT * FROM unified_person_coordination_queue ORDER BY created_at ASC").fetchall() + return [dict(row) for row in rows] + + +class UnifiedPersonReferenceMigration: + """Previewable additive migration; unknown legacy identities remain unresolved.""" + + def __init__(self, store: Any): + self.store = store + + async def preview(self, *, limit: int = 5000) -> dict[str, Any]: + return await self.store._run_recoverable_database_operation(self._preview_sync, max(1, int(limit or 1))) + + def _preview_sync(self, limit: int) -> dict[str, Any]: + rows = self.store._conn.execute( + """SELECT id, origin_identity_key, person_id, coordination_status FROM memories + WHERE origin_identity_key='' AND coordination_status='legacy_unresolved' + ORDER BY created_at ASC LIMIT ?""", + (limit,), + ).fetchall() + return { + "mode": "preview", "readonly": True, "migration_version": P1_MIGRATION_VERSION, + "would_mark_legacy_unresolved": len(rows), "items": [dict(row) for row in rows], + } + + async def apply(self, *, migration_id: str, limit: int = 5000, fault_after: int = 0) -> dict[str, Any]: + migration_id = clean_text(migration_id, 120) + if not migration_id: + return {"mode": "apply", "ok": False, "error_code": "migration_id_required", "changed": 0} + completed = await self.store._run_recoverable_database_operation(self._completed_result_sync, migration_id) + if completed is not None: + return completed + backup = self.store.backup(f".before_unified_person_p1.{uuid.uuid4().hex[:12]}") + return await self.store._run_recoverable_database_operation( + self._apply_sync, migration_id, max(1, int(limit or 1)), str(backup), max(0, int(fault_after or 0)) + ) + + def _completed_result_sync(self, migration_id: str) -> dict[str, Any] | None: + with self.store._lock: + existing = self.store._conn.execute( + "SELECT state, backup_path FROM unified_person_migration_runs WHERE migration_id=?", (migration_id,) + ).fetchone() + if not existing or str(existing["state"]) != "completed": + return None + return { + "mode": "apply", + "ok": True, + "migration_id": migration_id, + "changed": 0, + "idempotent": True, + "backup_path": str(existing["backup_path"]), + } + + def _apply_sync(self, migration_id: str, limit: int, backup_path: str, fault_after: int) -> dict[str, Any]: + if not migration_id: + return {"mode": "apply", "ok": False, "error_code": "migration_id_required", "changed": 0} + now = utc_now() + try: + with self.store._lock: + with self.store._transaction_sync(): + existing = self.store._conn.execute( + "SELECT state, backup_path FROM unified_person_migration_runs WHERE migration_id=?", (migration_id,) + ).fetchone() + if existing and str(existing["state"]) == "completed": + return {"mode": "apply", "ok": True, "migration_id": migration_id, "changed": 0, "idempotent": True, "backup_path": str(existing["backup_path"])} + self.store._conn.execute( + "INSERT OR REPLACE INTO unified_person_migration_runs(migration_id, migration_version, state, backup_path, created_at, updated_at) VALUES(?,?,?,?,COALESCE((SELECT created_at FROM unified_person_migration_runs WHERE migration_id=?),?),?)", + (migration_id, P1_MIGRATION_VERSION, "running", backup_path, migration_id, now, now), + ) + rows = self.store._conn.execute( + """SELECT id, person_id, origin_identity_key, coordination_status, projection_revision FROM memories + WHERE origin_identity_key='' AND coordination_status='legacy_unresolved' + ORDER BY created_at ASC LIMIT ?""", (limit,) + ).fetchall() + for index, row in enumerate(rows, start=1): + self.store._conn.execute( + "INSERT OR IGNORE INTO unified_person_migration_backup(migration_id, memory_id, previous_person_id, previous_origin_identity_key, previous_coordination_status, previous_projection_revision, created_at) VALUES(?,?,?,?,?,?,?)", + (migration_id, row["id"], row["person_id"], row["origin_identity_key"], row["coordination_status"], row["projection_revision"], now), + ) + self.store._conn.execute( + "UPDATE memories SET coordination_status='legacy_unresolved', updated_at=? WHERE id=?", (now, row["id"]) + ) + if fault_after and index >= fault_after: + raise RuntimeError("simulated_migration_interruption") + self.store._conn.execute( + "UPDATE unified_person_migration_runs SET state='completed', updated_at=? WHERE migration_id=?", (now, migration_id) + ) + return {"mode": "apply", "ok": True, "migration_id": migration_id, "changed": len(rows), "idempotent": False, "backup_path": backup_path} + except Exception as exc: + return {"mode": "apply", "ok": False, "migration_id": migration_id, "changed": 0, "error_code": "migration_interrupted", "error": clean_text(exc, 160), "backup_path": backup_path} + + async def rollback(self, migration_id: str) -> dict[str, Any]: + return await self.store._run_recoverable_database_operation(self._rollback_sync, clean_text(migration_id, 120)) + + def _rollback_sync(self, migration_id: str) -> dict[str, Any]: + if not migration_id: + return {"mode": "rollback", "ok": False, "error_code": "migration_id_required", "restored": 0} + with self.store._lock: + with self.store._transaction_sync(): + rows = self.store._conn.execute( + "SELECT * FROM unified_person_migration_backup WHERE migration_id=?", (migration_id,) + ).fetchall() + for row in rows: + self.store._conn.execute( + "UPDATE memories SET person_id=?, origin_identity_key=?, coordination_status=?, projection_revision=?, updated_at=? WHERE id=?", + (row["previous_person_id"], row["previous_origin_identity_key"], row["previous_coordination_status"], row["previous_projection_revision"], utc_now(), row["memory_id"]), + ) + self.store._conn.execute( + "UPDATE unified_person_migration_runs SET state='rolled_back', updated_at=? WHERE migration_id=?", (utc_now(), migration_id) + ) + return {"mode": "rollback", "ok": True, "migration_id": migration_id, "restored": len(rows)} diff --git a/docs/统一人物系统开发回执-20260727.md b/docs/统一人物系统开发回执-20260727.md new file mode 100644 index 0000000..4223c48 --- /dev/null +++ b/docs/统一人物系统开发回执-20260727.md @@ -0,0 +1,88 @@ +# OPS Unified Person Development Receipt (2026-07-27) + +## P2 Memory Person Layer and Firewall (2026-07-28) + +- Scope: P2 is implemented in this memory repository only. It adds independent `ops.person_memory.v2/2.0`; P0 `ops.unified_person.v1/1.0` remains a read-only nested projection with frozen descriptor, fixture, and fingerprint. +- Facts: additive fact status, observed/current person provenance, origin scope, correction/projection/firewall/fact event IDs, and firewall state. Current `person_id` may be reprojected; historical evidence and origin are retained. +- Migration: preview, physical pre-apply backup, idempotent apply, transaction interruption recovery, data rollback, and correction rollback are provided. Logical rollback retains additive schema; the backup is disaster recovery. +- Firewall/visibility: direct writes, structured envelopes, timeline, import, summaries, embeddings, relationship/knowledge edges, retrieval, and injection are gated. Blocked/rejected/quarantined/superseded content never enters ordinary FTS, embedding, edges, summaries, or injection. +- Authority: relationship-phase automatic advancement and phase-derived naming authority are retired from runtime; legacy phase state is compatibility evidence only. Person/relation/affinity/confinement authority remains companion-side. +- Verification: bundled-Python P2/P0/P1 directed suite last reported `24 passed`; compile, diff check, and P0 fingerprint checks passed. Existing `.pytest_cache` `WinError 5` is environmental; final full-suite audit is pending main-program review. +- Limits: no multimodal/tool gate, affinity/confinement work, pages, deployment, companion-repository changes, commit, push, or deployment. + +## Scope and status + +- Actual start: `ops-main` at `fde001a9c959880e11868b6cc7082b491e6c90ac`, clean working tree. +- Actual end: P0 unified-person contract consumer and migration-safe coordination foundation implemented in this repository only. +- No commit, push, deployment, VM change, root documentation change, or companion-repository write was performed. + +## Changed files + +- `core/unified_person_contract.py`: frozen contract descriptor, deterministic fingerprint, namespaced origin identity key, projection consumer, stable coordination results, and legacy-unresolved reference helper. +- `core/bridge.py`: read-only capability probe and explicit projection-consumer entry point. +- `tests/test_unified_person_contract.py`: fixed-sample, identity, error matrix, range, derived-band, Bridge-missing, and identity-conflict coverage. +- `tests/fixtures/unified_person_projection_v1.json`: byte-semantic JSON fixture aligned with the companion sample. +- `docs/错题本.md`: reusable contract-consumer lesson. + +## Contract and fixture + +- Contract: `ops.unified_person.v1` / `1.0`; sample version: `1.0`. +- Canonical descriptor keys: `name`, `version`, `sample_version`, `required_fields`, `enums`, `identity_key_fields`. +- Fingerprint: `633e2d235345d5c1bb72f8e6247e61932c5215ad2817242eefb709b502aefe03`. +- Fingerprint algorithm: SHA-256 over UTF-8 canonical JSON with sorted keys and compact separators; no timestamp, path, instance ID, random value, or fixture body participates. +- The local fixture is JSON-deep-equal to `astrbot_plugin_private_companion-main/docs/unified_person_projection_v1.sample.json`; it uses `person_d301001be7a1ce40df26a3eb`, revision `1`, Alice, score `12`, `hugging`, and the shared resolved identity key. + +## Data, migration, and coordination + +- This P0 change does not alter SQLite schemas and does not migrate, delete, merge, split, or create any existing person data. +- The normalized source identity requires all five fields: `companion_instance_id`, `bot_account_id`, `adapter_instance_id`, `subject_namespace`, and `platform_subject_id`. It accepts only `str` or non-boolean `int`, normalizes Unicode NFC, trims only leading and trailing whitespace, preserves internal whitespace, rejects NUL and values longer than 160 characters, and lowercases only `subject_namespace`. Float, NaN, and Infinity values are rejected so one numeric identity cannot acquire multiple key encodings. Its origin key is immutable and never falls back to a bare platform ID. +- `relationship_capabilities` is fingerprinted in the shared enums and accepts only the frozen capability values. Required projection strings, aliases, and resolved identity keys are strictly rejected when malformed, NUL-containing, or overlong; accepted `person_id` values are returned unchanged. +- Consumer statuses are `resolved`, `pending_companion`, `contract_mismatch`, `identity_conflict`, `quarantined`, and `legacy_unresolved`. +- Bridge absence returns `pending_companion` with `bridge_unavailable`; malformed or mismatched inputs return a stable error and no `person_id`. +- P1 persistent identity/reference columns, a durable pending queue, dry-run/backup/rollback migration, and write/recall firewall integration remain pending. They require a separately reviewed schema plan. + +## Runtime and verification + +- Existing memory request hook priority remains `-20`; this change adds no hook and does not alter capture, retrieval, injection, or existing bridge write paths. +- Directed: `C:\Users\Administrator\.cache\codex-runtimes\codex-primary-runtime\dependencies\python\python.exe -m pytest tests/test_unified_person_contract.py -q` -> `7 passed`. +- Full: the same Python runtime with `-m pytest -q` -> `338 passed, 8 subtests passed`. +- Fixture equality: a JSON deep comparison against the companion sample passed. +- `C:\Users\Administrator\.cache\codex-runtimes\codex-primary-runtime\dependencies\python\python.exe -m compileall -q .` passed. It reported only that the existing `.pytest_cache` directory could not be listed. +- `git diff --check` passed. +- Pytest emitted the pre-existing `.pytest_cache` permission warning (`WinError 5`); it did not affect results. + +## Counterexamples and cross-plugin dependencies + +- Counterexamples prove rejection of bad name/version/fingerprint, missing fields, unknown enums, float/out-of-range affinity, inconsistent affinity band, zero/string revision, invalid required text, invalid optional-string types, unknown capability, identity conflict, and Bridge absence. They also prove NFC identity equivalence, internal-whitespace preservation, scalar rejection, zero identity preservation, and strict NUL/length/key-format rejection. +- The companion side must keep exposing exactly `probe_unified_person_contract()` compatible descriptor data and a projection with the frozen fields, capability values, strict integer affinity/revision semantics, and `ops-origin-v1:` resolved identity key. It remains the only authority for person creation, identity link changes, relation policy, owner state, affinity, warnings, confinement, and final safety decisions. +- Memory-side consumers must remain read-only until the next reviewed migration and firewall phase; the master controller should compare this descriptor, fingerprint, fixture, and Bridge behavior before enabling cross-plugin flow. + +## P1 persistence implementation and audit + +- Scope: this repository now persists only the companion-authoritative `person_id` reference. It does not create, merge, split, or infer people, and it does not access the companion database. +- `core/unified_person_persistence.py` adds additive SQLite schema installation, five-field source identities, immutable `origin_identity_key`, a durable coordination queue, lease-based replay, and a previewable reference migration. +- `MemoryRecord` persists `person_id`, `origin_identity_key`, `coordination_status`, and `projection_revision`. New in-process records default to `pending_companion`; pre-existing SQLite rows receive the conservative schema default `legacy_unresolved`. +- A successful companion projection is validated through the frozen P0 consumer and records the five-field source identity. Merge, split, and revision events only reproject the current `person_id` and revision. They never rewrite evidence or an existing origin key. +- Bridge absence, contract mismatch, identity conflict, and invalid identity are queued or quarantined with stable error state. Queue keys use the supplied operation ID, or the stable `(memory_id, origin_identity_key)` fallback; status changes do not create new work rows. A confirmed queue row and an already resolved memory cannot be downgraded by a later failed retry. +- Queue claims require a retryable status and an expired or absent lease. Replay requires the exact queue ID and token, an unexpired lease, and a retryable state; expired tokens and confirmed rows are rejected. New databases use `memory_id ... REFERENCES memories(id) ON DELETE CASCADE`; existing tables also receive explicit queue cleanup on single deletion, bulk clear, and import rollback paths. +- Source identities have both the immutable origin-key primary key and five-field unique constraint. A SQLite trigger rejects direct changes to a nonempty `origin_identity_key`. +- `UnifiedPersonReferenceMigration` supports dry-run, pre-apply database backup, transaction rollback on simulated interruption, idempotent completed runs, and data rollback. Normal rollback restores saved row values but intentionally leaves the additive P1 columns, tables, indexes, and trigger in place; the full pre-apply backup is the physical-disaster-recovery artifact. +- P1 tests cover schema constraints and trigger behavior, revision monotonicity, queue deduplication, concurrent claiming, expired lease rejection, confirmed-state preservation, mismatch isolation, deletion cleanup, migration interruption, backup, idempotence, and rollback. + +## P1 verification + +- Directed P0+P1 tests: `15 passed`. +- Independent final full suite: `346 passed, 8 subtests passed`. +- `python -m compileall -q .` and `git diff --check` passed. Compileall could not list the pre-existing `.pytest_cache`; pytest emitted the same `WinError 5` cache-write warning. Neither affected test execution. +- P0 contract fingerprint remains exactly `633e2d235345d5c1bb72f8e6247e61932c5215ad2817242eefb709b502aefe03`. +- No commit, push, deployment, VM change, root-document write, or companion-repository write was performed. +## P2 记忆人物层与文本防火墙(2026-07-28) + +- 范围:新增独立 `ops.person_memory.v2/2.0` envelope,P0 `ops.unified_person.v1/1.0` 仅作为可选只读嵌套投影,未修改 P0 descriptor、fixture 或指纹。 +- 数据:`memories` additive 增加 fact/provenance/origin scope/correction/projection/firewall 字段、不可变 observed person 触发器、纠错事件唯一索引和 P2 migration journal/backup。 +- 门禁:Store 统一消费结构化 P2 envelope;blocked/rejected/quarantined/superseded 记录不进入 FTS、embedding、关系边或普通召回。私聊同源召回允许,私聊到群聊、跨群和低保证路径保守拒绝。 +- 纠错:纠错事件幂等,新事实 supersede 旧事实而不改写旧 evidence/origin;旧 FTS、embedding、relationship edge、knowledge edge 同步失效。 +- 权威:relationship phase 自动推进和基于阶段的称呼注入退役为 no-op;legacy state 只读兼容。记忆仓不创建、合并或拆分 person。 +- 迁移:preview/apply/backup/idempotence/rollback 已提供;中断返回稳定 `interrupted`,事务回滚数据 journal,物理备份仅用于灾难恢复,常规 rollback 保留 additive schema。 +- 验证:主程序独立复跑 `tests/test_person_memory_p2.py tests/test_unified_person_contract.py tests/test_unified_person_persistence.py -q` 为 `27 passed`;最终全量为 `358 passed, 8 subtests passed`。`python -m compileall -q .` 与 `git diff --check` 均成功退出;仅有既有 `.pytest_cache` 的 `WinError 5` 缓存写入/列举警告,未影响执行。P0 指纹仍为 `633e2d235345d5c1bb72f8e6247e61932c5215ad2817242eefb709b502aefe03`。 +- 限制:未涉及多模态/工具门、好感/禁闭、页面或部署;陪伴仓已通过 `_memory_companion_unified_person_authority -> session_context -> bridge.compose_context -> service.session_context_from_bridge` 提供真实 direct compose P0 projection source,默认配置为空时 fail closed。event attribute 注入尚未实现,且非本轮必需范围;没有提交、推送或部署。 diff --git a/docs/错题本.md b/docs/错题本.md index 18b93bb..846c633 100644 --- a/docs/错题本.md +++ b/docs/错题本.md @@ -1,5 +1,16 @@ # 错题本 +## 2026-07-28 - P2 人物事实双侧防火墙 + +- 阶段:REQ-003 / DEV-003 P2 记忆人物层。 +- 问题:独立 envelope 若未被真实写入入口消费,阻断决定仍可能绕过索引与召回;`pending` 默认值也会误伤正常 allowed 写入。 +- 防范:Store 统一消费结构化 P2 envelope,普通 allowed 本地写入显式进入 confirmed;blocked/rejected/quarantined/superseded 同时从 FTS、embedding、边和普通召回面移除。 +- 问题:纠错只 supersede 原事实会遗留引用旧 memory ID 的关系、知识或摘要;模糊字符串匹配会误伤无关摘要。 +- 防范:纠错及回滚记录审计状态,清理原事实和 replacement 的派生面;只解析并精确匹配约定 `source_memory_id` / `source_memory_ids` 后失效、恢复派生摘要。 +- 问题:人物跨渠道召回不能将裸平台 user ID 当成 person,也不能永久禁止已验证同 person 私聊共享。 +- 防范:Bridge context 仅消费验证 P0 projection,保留 person、origin 与 assurance;缺投影、低 assurance、跨 person、私聊到群聊和 mismatch 均 fail closed。 +- 验证与限制:P2 定向、P0 指纹和 compile 检查已执行,最终全量由主程序审计;陪伴仓已接通 `_memory_companion_unified_person_authority -> session_context -> bridge.compose_context -> service.session_context_from_bridge` 的真实 direct compose P0 projection source,默认配置为空时 fail closed。event attribute 注入尚未实现,且非本轮必需范围;未提交、推送或部署,未进入多模态、工具门、好感/禁闭、页面或部署范围。 + ## 2026-07-22 — 测试仍引用插件旧包名 - 阶段:源码复盘与回归验证 @@ -251,3 +262,43 @@ - 预防规则:删除或替换后端能力时,必须同批核对实现、路由、配置、HTML、JS、测试和文档;测试应保护目标状态,不应保护被退役的入口。 - 验证证据:`python -m unittest tests.test_panel_regressions` 9/9 通过;`node --check pages/记忆面板/app.js` 通过;面板源码无 `LivingMemory`、旧路径或旧按钮 ID。 - 部署验证补充:提交 `c9106d9` 已推送并于 2026-07-27 12:27:05 CST 增量部署;服务 `active`、`NRestarts=0`,四个部署文件哈希一致。 + +## 2026-07-27 — 统一人物投影消费者必须逐字段对齐生产者 + +- 阶段:OPS 统一人物系统 P0 +- 类型:跨插件合同 / 身份隔离 / 测试 +- 现象:只验证合同名称、版本和指纹仍会接受生产者不会输出的浮点好感值、错误好感档位、零修订号或不完整文本字段;独立夹具也会掩盖两个仓样例漂移。 +- 根因:消费者初版只把投影视为松散字典,没有把生产者的严格 `projection_revision`、整数好感范围、档位映射、能力白名单和必填文本约束变成同等级门禁。 +- 修复或规避:新增 `core/unified_person_contract.py`,使用冻结 descriptor 的 SHA-256 指纹;完整身份五元组生成不可变 `ops-origin-v1:` 键;消费者只返回只读投影或稳定协调状态,绝不按平台 ID 创建人物。固定 JSON 样例与陪伴仓样例深度相等,并覆盖合同错配、字段/枚举错误、身份冲突和 Bridge 缺失。 +- 预防规则:跨插件投影必须同时比较 canonical descriptor、指纹和固定 JSON;数值、枚举、必填字符串和派生字段的消费者规则不得弱于生产者;Bridge 不可用只能进入 `pending_companion`,不能推断 `person_id`。 +- 验证证据:最终 `tests/test_unified_person_contract.py` 7 项通过;全仓 `338 passed, 8 subtests passed`;共享样例深度相等;编译和 diff 检查见本次开发回执。 + +## 2026-07-27 — 身份键不能走通用文本截断或弱枚举合同 + +- 阶段:OPS 统一人物系统 P0 审计修复 +- 类型:身份完整性 / 跨插件合同 / 测试 +- 现象:通用 `clean_text` 会折叠内部空白并可能截断身份字段;消费者还曾把能力白名单放在代码中却没有纳入指纹,并会在验证后截断超长 `person_id`。 +- 根因:身份键和权威投影误复用了面向展示与日志的文本清洗策略,且能力集合没有作为冻结合同的一部分参与两仓一致性校验。 +- 修复或规避:身份五元组改用 NFC、仅首尾裁剪、160 字符上限、NUL/容器/bool/float 拒绝和 namespace 小写;`0` 保留为 `"0"`。能力值进入 `enums.relationship_capabilities`;所有权威投影字符串严格拒绝而不截断,resolved key 必须匹配 `ops-origin-v1:` 加 64 位小写 hex。 +- 预防规则:用于哈希或跨插件主键的值不得经过展示型清洗;任何实际校验的枚举都必须参与 descriptor 指纹;验证成功后的权威标识必须原样返回。 +- 验证证据:新增 NFC、内部空白、容器/bool/float/NaN/Infinity、零值、超长、NUL、坏 identity key、别名和能力枚举反例;定向 7 项通过,全仓 `338 passed, 8 subtests passed`。 + +## 2026-07-27 — 持久协调队列的状态变化不能改变幂等身份 + +- 阶段:OPS 统一人物系统 P1 +- 类型:持久化 / 并发 / 数据完整性 +- 现象:如果把协调状态纳入队列幂等键,同一个 Bridge 事件从断连变为合同错误时会产生多条待办;若失败分支无条件更新记忆状态,又会把已确认的人物引用降级。只在选取阶段检查租约还会允许旧 token 在租约过期后完成回放。 +- 根因:把事件的稳定身份、队列状态机和最终人物投影混为一次普通 upsert,缺少对 confirmed/resolved 终态与 lease 所有权的完整条件约束。 +- 修复或规避:队列 key 只使用 operation ID,或 `(memory_id, origin_identity_key)` 回退;confirmed 行不可被失败重写,带有效 resolved `person_id` 的记忆不可降级。claim 与 replay 的 SQL 都同时限制 retryable status、queue ID、token 和未过期 lease;来源键由触发器固定,队列通过新库 FK 与旧库显式删除清理共同防孤儿行。 +- 预防规则:幂等键只能描述业务事件而不能包含可迁移状态;任何终态保护和租约保护必须同时出现在读写两侧的条件中。迁移的逻辑 rollback 只还原数据,物理 schema 恢复依赖预先生成的完整数据库备份。 +- 验证证据:P1 定向加 P0 合同测试 `15 passed`;独立全量验证 `346 passed, 8 subtests passed`;触发器、五字段 UNIQUE、FK、删除清理、过期 token、确认后断连和迁移中断/回滚均有覆盖。 +## 2026-07-28 P2:人物事实必须在写入和召回两侧防火墙 + +- 问题:只在独立 validator 验证 P2 envelope,实际 `insert_memory` 仍可绕过,且 `pending` 默认值会让正常允许写入被召回门误拒。 +- 防范:Store 写入门消费 `metadata.person_memory_envelope`,普通 allowed 本地写入明确转为 `confirmed`;blocked/rejected/quarantined/superseded 同时剔除 FTS、embedding 和派生边。 +- 问题:只凭不存在于运行时的 `ctx.person_id` 判断人物,会让同源私聊已解析记录全部 low-assurance。 +- 防范:保留显式 verified `ctx.person_id`,并允许同一私聊 platform subject;群聊、跨群、跨人物仍 fail-closed,定向 RetrievalEngine 测试覆盖。 +- 问题:纠错若只更新原记录状态,会留下旧关系/知识派生可被间接使用。 +- 防范:纠错事务同时失效旧 FTS、embedding、relationship_edges 和 knowledge_edges;`correction_event_id` 唯一保证重放不复制事实。 +- 问题:迁移中断若抛异常,调用端无法稳定判定且易误认为半成功。 +- 防范:P2 migration 在事务内执行并将受控中断转换成 `ok=false,error_code=interrupted`;journal/行备份随事务回滚,物理 SQLite backup 保留作灾难恢复。 diff --git a/tests/fixtures/unified_person_projection_v1.json b/tests/fixtures/unified_person_projection_v1.json new file mode 100644 index 0000000..818489c --- /dev/null +++ b/tests/fixtures/unified_person_projection_v1.json @@ -0,0 +1,25 @@ +{ + "contract_name": "ops.unified_person.v1", + "contract_version": "1.0", + "contract_fingerprint": "633e2d235345d5c1bb72f8e6247e61932c5215ad2817242eefb709b502aefe03", + "projection_revision": 1, + "person_id": "person_d301001be7a1ce40df26a3eb", + "resolved_identity_key": "ops-origin-v1:9d1e0a54d988272076d6c76bfd01a4eb8b7ba2b9d06f7d014149ff37659344d6", + "identity_assurance": "verified", + "profile_status": "active", + "display_name": "Alice", + "aliases": ["Alice"], + "relation_policy_id": "default_friend", + "relation_label": "friend", + "relationship_capabilities": ["hugging"], + "owner_mode": "not_owner", + "affinity_score": 12, + "affinity_band": "positive", + "warning_stage": "none", + "confinement_state": "none", + "confinement_until": "", + "release_prompt_pending": false, + "proactive_private_enabled": false, + "group_overlay_ref": "", + "updated_at": "2026-07-27T00:00:00+00:00" +} diff --git a/tests/test_person_memory_p2.py b/tests/test_person_memory_p2.py new file mode 100644 index 0000000..8aef02e --- /dev/null +++ b/tests/test_person_memory_p2.py @@ -0,0 +1,303 @@ +from __future__ import annotations + +import asyncio +import json +import shutil +from pathlib import Path + +import pytest + +from core.models import EntityRef, MemoryRecord, SessionContext +from core.person_memory import PersonMemoryFacts, PersonMemoryMigration +from core.person_memory_contract import ( + CONTRACT_NAME, CONTRACT_VERSION, consume_person_memory_envelope, contract_fingerprint, +) +from core.retrieval import RetrievalEngine +from core.store import MemoryStore +from core.unified_person_contract import contract_fingerprint as p0_fingerprint +from core.visibility import VisibilityPolicy + + +P0_FINGERPRINT = "633e2d235345d5c1bb72f8e6247e61932c5215ad2817242eefb709b502aefe03" + + +def _store(name: str) -> tuple[MemoryStore, Path]: + root = Path.cwd() / ".tmp" / f"p2-{name}" + shutil.rmtree(root, ignore_errors=True) + root.mkdir(parents=True, exist_ok=True) + store = MemoryStore(root / "memory.db") + store.initialize() + return store, root + + +def _record(identifier: str, **changes: object) -> MemoryRecord: + base = MemoryRecord( + id=identifier, memory_type="user_fact", memory_domain="user_memory", scope="private", + session_id="private:alice", platform="test", subject=EntityRef(id="alice"), + content="alice prefers tea", evidence="message", visibility="private_pair", + ) + for key, value in changes.items(): setattr(base, key, value) + return base + + +def _envelope(event_id: str, **changes: object) -> dict[str, object]: + payload: dict[str, object] = { + "contract_name": CONTRACT_NAME, + "contract_version": CONTRACT_VERSION, + "contract_fingerprint": contract_fingerprint(), + "event_id": event_id, + "event_kind": "fact", + "fact_status": "confirmed", + "firewall_status": "allowed", + "origin_scope": "private", + } + payload.update(changes) + return payload + + +def test_p2_contract_requires_event_and_does_not_drift_p0() -> None: + assert p0_fingerprint() == P0_FINGERPRINT + payload = { + "contract_name": CONTRACT_NAME, "contract_version": CONTRACT_VERSION, + "contract_fingerprint": contract_fingerprint(), "event_kind": "fact", + "fact_status": "confirmed", "firewall_status": "allowed", "origin_scope": "private", + } + assert consume_person_memory_envelope(payload)["error_code"] == "invalid_field" + payload.update(event_id="evt-1", correction_event_id="corr", projection_event_id="proj", firewall_event_id="fw") + accepted = consume_person_memory_envelope(payload) + assert accepted["accepted"] and accepted["envelope"]["correction_event_id"] == "corr" + + +def test_write_firewall_and_recall_gate() -> None: + store, root = _store("gate") + try: + asyncio.run(store.insert_memory(_record("allowed"))) + asyncio.run(store.insert_memory(_record("blocked", content="blocked confidential text", firewall_status="blocked"))) + rows = store._conn.execute("SELECT id, fact_status, firewall_status FROM memories ORDER BY id").fetchall() + assert [tuple(row) for row in rows] == [("allowed", "confirmed", "allowed"), ("blocked", "rejected", "blocked")] + assert store._conn.execute("SELECT memory_id FROM memory_fts WHERE memory_id='blocked'").fetchone() is None + retriever = RetrievalEngine(store, VisibilityPolicy()) + found = asyncio.run(retriever.search("tea", SessionContext(scope="private", session_id="private:alice", user_id="alice"))) + assert [item.memory.id for item in found] == ["allowed"] + finally: + store.close(); shutil.rmtree(root, ignore_errors=True) + + +def test_envelope_is_consumed_by_actual_store_write_gate() -> None: + store, root = _store("envelope") + try: + envelope = { + "contract_name": CONTRACT_NAME, "contract_version": CONTRACT_VERSION, + "contract_fingerprint": contract_fingerprint(), "event_id": "fw-event", + "event_kind": "firewall", "fact_status": "confirmed", "firewall_status": "blocked", + "origin_scope": "private", "firewall_event_id": "fw-event", + } + record = _record("enveloped", content="must not index", metadata={"person_memory_envelope": envelope}) + asyncio.run(store.insert_memory(record)) + row = store._conn.execute("SELECT fact_status, firewall_status, firewall_event_id FROM memories WHERE id='enveloped'").fetchone() + assert tuple(row) == ("rejected", "blocked", "fw-event") + assert store._conn.execute("SELECT 1 FROM memory_fts WHERE memory_id='enveloped'").fetchone() is None + finally: + store.close(); shutil.rmtree(root, ignore_errors=True) + + +def test_nested_p0_projection_binds_current_person_without_rewriting_observed() -> None: + store, root = _store("projection") + try: + projection = json.loads((Path(__file__).parent / "fixtures" / "unified_person_projection_v1.json").read_text(encoding="utf-8")) + source = {"companion_instance_id": "companion-a", "bot_account_id": "bot-1", "adapter_instance_id": "telegram-main", "subject_namespace": "telegram.user", "platform_subject_id": "42"} + envelope = {"contract_name": CONTRACT_NAME, "contract_version": CONTRACT_VERSION, "contract_fingerprint": contract_fingerprint(), "event_id": "projection-1", "event_kind": "fact", "fact_status": "confirmed", "firewall_status": "allowed", "origin_scope": "private", "source_identity": source, "p0_projection": projection} + record = _record("projected", evidence="immutable evidence", metadata={"person_memory_envelope": envelope}) + asyncio.run(store.insert_memory(record)) + first = store._conn.execute("SELECT person_id, observed_person_id, origin_identity_key, projection_revision, evidence FROM memories WHERE id='projected'").fetchone() + projection["person_id"] = "person-merged"; projection["projection_revision"] = 2 + envelope["event_id"] = "projection-2" + # A fresh incoming DTO must not overwrite immutable observed/origin/evidence. + asyncio.run(store.insert_memory(_record("projected", evidence="replacement evidence", metadata={"person_memory_envelope": envelope}))) + second = store._conn.execute("SELECT person_id, observed_person_id, origin_identity_key, projection_revision, evidence FROM memories WHERE id='projected'").fetchone() + assert tuple(first) == tuple(second) + finally: + store.close(); shutil.rmtree(root, ignore_errors=True) + + +def test_concurrent_same_id_p2_replays_are_zero_write() -> None: + store, root = _store("concurrent-replay") + try: + asyncio.run(store.insert_memory(_record( + "same-id", content="original content", evidence="original evidence", + metadata={"person_memory_envelope": _envelope("create-event")}, + ))) + replay_one = _record( + "same-id", content="replay one", evidence="replay one evidence", + metadata={"person_memory_envelope": _envelope("replay-event-one")}, + ) + replay_two = _record( + "same-id", content="replay two", evidence="replay two evidence", + metadata={"person_memory_envelope": _envelope("replay-event-two")}, + ) + + async def replay() -> list[str]: + return await asyncio.gather(store.insert_memory(replay_one), store.insert_memory(replay_two)) + + assert asyncio.run(replay()) == ["same-id", "same-id"] + row = store._conn.execute( + "SELECT content, evidence, fact_event_id FROM memories WHERE id='same-id'" + ).fetchone() + assert tuple(row) == ("original content", "original evidence", "create-event") + finally: + store.close(); shutil.rmtree(root, ignore_errors=True) + + +def test_timeline_historical_import_and_summary_paths_reject_blocked_decisions() -> None: + store, root = _store("entry-gates") + try: + assert asyncio.run(store.add_timeline_event(event_type="turn", session_id="private:a", scope="private", subject_id="a", object_id="", content="secret", metadata={"firewall_status": "blocked"})) == "" + assert asyncio.run(store.add_historical_timeline_events([{"id": "imported", "event_type": "turn", "session_id": "private:a", "scope": "private", "subject_id": "a", "message_id": "m", "content": "secret", "metadata": {"firewall_status": "quarantined"}}])) == {} + summary = _record("summary", memory_type="conversation_summary", content="blocked summary", firewall_status="quarantined") + asyncio.run(store.insert_memory(summary)) + assert store._conn.execute("SELECT COUNT(*) FROM timeline").fetchone()[0] == 0 + assert store._conn.execute("SELECT 1 FROM memory_fts WHERE memory_id='summary'").fetchone() is None + finally: + store.close(); shutil.rmtree(root, ignore_errors=True) + + +def test_timeline_and_historical_import_reject_invalid_p2_envelopes_without_writes() -> None: + store, root = _store("timeline-envelope-gates") + try: + invalid_contract = _envelope("bad-contract", contract_name="ops.person_memory.v999") + invalid_fingerprint = _envelope("bad-fingerprint", contract_fingerprint="not-a-contract-fingerprint") + blocked = _envelope("blocked-event", firewall_status="blocked") + assert asyncio.run(store.add_timeline_event( + event_type="turn", session_id="private:a", scope="private", subject_id="a", object_id="", + content="invalid contract", metadata={"person_memory_envelope": invalid_contract}, + )) == "" + assert asyncio.run(store.add_timeline_event( + event_type="turn", session_id="private:a", scope="private", subject_id="a", object_id="", + content="invalid fingerprint", metadata={"person_memory_envelope": invalid_fingerprint}, + )) == "" + assert asyncio.run(store.add_timeline_event( + event_type="turn", session_id="private:a", scope="private", subject_id="a", object_id="", + content="accepted but blocked", metadata={"firewall_status": "allowed", "person_memory_envelope": blocked}, + )) == "" + inserted = asyncio.run(store.add_historical_timeline_events([ + {"id": "bad-contract", "event_type": "turn", "session_id": "private:a", "scope": "private", "subject_id": "a", "message_id": "bad-contract", "content": "invalid contract", "metadata": {"person_memory_envelope": invalid_contract}}, + {"id": "bad-fingerprint", "event_type": "turn", "session_id": "private:a", "scope": "private", "subject_id": "a", "message_id": "bad-fingerprint", "content": "invalid fingerprint", "metadata": {"person_memory_envelope": invalid_fingerprint}}, + {"id": "accepted-blocked", "event_type": "turn", "session_id": "private:a", "scope": "private", "subject_id": "a", "message_id": "accepted-blocked", "content": "accepted but blocked", "metadata": {"firewall_status": "allowed", "person_memory_envelope": blocked}}, + ])) + assert inserted == {} + assert store._conn.execute("SELECT COUNT(*) FROM timeline").fetchone()[0] == 0 + finally: + store.close(); shutil.rmtree(root, ignore_errors=True) + + +def test_resolved_private_person_is_recalled_for_same_session_subject() -> None: + store, root = _store("private-recall") + try: + asyncio.run(store.insert_memory(_record("person-private", content="alice has a blue notebook", person_id="person-1", coordination_status="resolved"))) + retriever = RetrievalEngine(store, VisibilityPolicy()) + found = asyncio.run(retriever.search("blue notebook", SessionContext(scope="private", session_id="private:alice", platform="test", user_id="alice", person_id="person-1"))) + assert [item.memory.id for item in found] == ["person-private"] + group = asyncio.run(retriever.search("blue notebook", SessionContext(scope="group", session_id="group:g", group_id="g", user_id="alice"))) + assert not group + finally: + store.close(); shutil.rmtree(root, ignore_errors=True) + + +def test_correction_is_idempotent_and_keeps_original_evidence() -> None: + store, root = _store("correction") + try: + asyncio.run(store.insert_memory(_record("old", evidence="original evidence", person_id="person-1", coordination_status="resolved"))) + asyncio.run(store.upsert_relationship(subject=EntityRef(id="alice"), object=EntityRef(id="bob"), relation_type="knows", source_memory_id="old")) + source = asyncio.run(store.upsert_knowledge_node(node_type="person", label="alice")) + target = asyncio.run(store.upsert_knowledge_node(node_type="person", label="bob")) + asyncio.run(store.upsert_knowledge_edge(source_node_id=source, target_node_id=target, relation_type="knows", source_memory_id="old")) + asyncio.run(store.insert_memory(_record("derived-summary", memory_type="memory_decay_summary", content="derived", metadata={"source_memory_ids": ["old"]}))) + asyncio.run(store.upsert_memory_embedding(memory_id="derived-summary", provider_id="test", text_hash="derived", vector=[0.1, 0.2])) + facts = PersonMemoryFacts(store) + first = asyncio.run(facts.correct(memory_id="old", correction_event_id="corr-1", replacement=_record("new", content="alice prefers coffee", evidence="correction evidence"))) + replay = asyncio.run(facts.correct(memory_id="old", correction_event_id="corr-1", replacement=_record("other"))) + old = store._conn.execute("SELECT evidence, fact_status FROM memories WHERE id='old'").fetchone() + new = store._conn.execute("SELECT supersedes_id, observed_person_id FROM memories WHERE id=?", (first["record_id"],)).fetchone() + assert (first["deduplicated"], replay["deduplicated"], tuple(old), tuple(new)) == (False, True, ("original evidence", "superseded"), ("old", "person-1")) + assert store._conn.execute("SELECT 1 FROM relationship_edges WHERE source_memory_id='old'").fetchone() is None + assert store._conn.execute("SELECT 1 FROM knowledge_edges WHERE source_memory_id='old'").fetchone() is None + assert store._conn.execute("SELECT fact_status FROM memories WHERE id='derived-summary'").fetchone()[0] == "superseded" + assert store._conn.execute("SELECT 1 FROM memory_embeddings WHERE memory_id='derived-summary'").fetchone() is None + rolled = asyncio.run(facts.rollback_correction("corr-1")) + assert rolled["ok"] and store._conn.execute("SELECT fact_status FROM memories WHERE id='old'").fetchone()[0] == "confirmed" + assert store._conn.execute("SELECT fact_status FROM memories WHERE id='derived-summary'").fetchone()[0] == "confirmed" + assert store._conn.execute("SELECT 1 FROM memory_embeddings WHERE memory_id='derived-summary'").fetchone() is not None + assert store._conn.execute("SELECT 1 FROM memory_fts WHERE memory_id='new'").fetchone() is None + finally: + store.close(); shutil.rmtree(root, ignore_errors=True) + + +def test_correction_rejects_noncurrent_targets_and_out_of_order_rollback() -> None: + store, root = _store("correction-state-machine") + try: + facts = PersonMemoryFacts(store) + for identifier, status in (("pending", "pending"), ("rejected", "rejected"), ("quarantined", "quarantined")): + changes: dict[str, object] = {"fact_status": status} + if status == "pending": + changes["person_id"] = "unresolved-person" + asyncio.run(store.insert_memory(_record(identifier, content=f"{identifier} content", **changes))) + before = store._conn.execute("SELECT COUNT(*) FROM memories").fetchone()[0] + rejected = asyncio.run(facts.correct( + memory_id=identifier, + correction_event_id=f"corr-{identifier}", + replacement=_record(f"replacement-{identifier}"), + )) + assert rejected == {"ok": False, "error_code": "correction_target_not_current"} + assert store._conn.execute("SELECT COUNT(*) FROM memories").fetchone()[0] == before + + asyncio.run(store.insert_memory(_record("original"))) + first = asyncio.run(facts.correct( + memory_id="original", correction_event_id="corr-first", replacement=_record("first"), + )) + second = asyncio.run(facts.correct( + memory_id=first["record_id"], correction_event_id="corr-second", replacement=_record("second"), + )) + statuses = store._conn.execute( + "SELECT id, fact_status FROM memories WHERE id IN ('original', 'first', 'second') ORDER BY id" + ).fetchall() + assert [tuple(row) for row in statuses] == [ + ("first", "superseded"), ("original", "superseded"), ("second", "confirmed"), + ] + before_rollback = [tuple(row) for row in statuses] + out_of_order = asyncio.run(facts.rollback_correction("corr-first")) + after_rollback = store._conn.execute( + "SELECT id, fact_status FROM memories WHERE id IN ('original', 'first', 'second') ORDER BY id" + ).fetchall() + assert out_of_order == {"ok": False, "error_code": "correction_conflict"} + assert [tuple(row) for row in after_rollback] == before_rollback + assert asyncio.run(facts.rollback_correction("corr-second"))["ok"] + assert asyncio.run(facts.rollback_correction("corr-first"))["ok"] + assert store._conn.execute("SELECT fact_status FROM memories WHERE id='original'").fetchone()[0] == "confirmed" + finally: + store.close(); shutil.rmtree(root, ignore_errors=True) + + +def test_migration_interruption_rolls_back_rows_and_reports_stably() -> None: + store, root = _store("migration") + try: + asyncio.run(store.insert_memory(_record("legacy", fact_status="legacy"))) + migration = PersonMemoryMigration(store) + result = asyncio.run(migration.apply(migration_id="stop", interrupt_after=1)) + assert (result["ok"], result["error_code"]) == (False, "interrupted") + assert store._conn.execute("SELECT COUNT(*) FROM person_memory_migration_runs WHERE migration_id='stop'").fetchone()[0] == 0 + assert store._conn.execute("SELECT fact_status FROM memories WHERE id='legacy'").fetchone()[0] == "legacy" + finally: + store.close(); shutil.rmtree(root, ignore_errors=True) + + +def test_migration_apply_then_rollback_restores_observed_provenance() -> None: + store, root = _store("migration-rollback") + try: + asyncio.run(store.insert_memory(_record("legacy-person", fact_status="legacy", person_id="person-1", coordination_status="resolved"))) + migration = PersonMemoryMigration(store) + applied = asyncio.run(migration.apply(migration_id="apply-rollback")) + assert applied["ok"] and store._conn.execute("SELECT observed_person_id FROM memories WHERE id='legacy-person'").fetchone()[0] == "person-1" + rolled = asyncio.run(migration.rollback("apply-rollback")) + assert rolled["ok"] and store._conn.execute("SELECT fact_status, observed_person_id FROM memories WHERE id='legacy-person'").fetchone()[0] == "legacy" + finally: + store.close(); shutil.rmtree(root, ignore_errors=True) diff --git a/tests/test_unified_person_contract.py b/tests/test_unified_person_contract.py new file mode 100644 index 0000000..a640d7c --- /dev/null +++ b/tests/test_unified_person_contract.py @@ -0,0 +1,154 @@ +from __future__ import annotations + +import json +from pathlib import Path + +from core.bridge import MemoryCompanionBridge +from core.unified_person_contract import ( + CONTRACT_NAME, + CONTRACT_VERSION, + contract_descriptor, + contract_fingerprint, + consume_unified_person_projection, + legacy_unresolved_reference, + normalize_source_identity, + origin_identity_key, +) + + +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", +} + + +def _sample() -> dict: + return json.loads(FIXTURE_PATH.read_text(encoding="utf-8")) + + +def test_contract_descriptor_and_fingerprint_are_deterministic(): + descriptor = contract_descriptor() + assert descriptor["name"] == CONTRACT_NAME + assert descriptor["version"] == CONTRACT_VERSION + assert descriptor["sample_version"] == "1.0" + assert descriptor["required_fields"] == sorted(descriptor["required_fields"]) + assert contract_fingerprint() == _sample()["contract_fingerprint"] + assert len(contract_fingerprint()) == 64 + + +def test_fixed_json_projection_consumes_without_creating_a_person(): + payload = _sample() + result = consume_unified_person_projection(payload, source_identity=SOURCE_IDENTITY) + assert result["accepted"] is True + assert result["status"] == "resolved" + assert result["person_id"] == "person_d301001be7a1ce40df26a3eb" + assert result["origin_identity_key"] == payload["resolved_identity_key"] + assert result["projection"]["owner_mode"] == "not_owner" + + +def test_source_identity_key_requires_all_namespaced_fields_and_is_stable(): + source = SOURCE_IDENTITY + key = origin_identity_key(source) + assert key == origin_identity_key({**source, "subject_namespace": "TELEGRAM.USER"}) + assert key != origin_identity_key({**source, "adapter_instance_id": "adapter-b"}) + try: + origin_identity_key({"platform_subject_id": "bare-platform-id"}) + except ValueError as exc: + assert str(exc) == "invalid_identity" + else: + raise AssertionError("incomplete identity must fail") + + +def test_source_identity_normalization_matches_companion_scalar_rules(): + composed = {**SOURCE_IDENTITY, "adapter_instance_id": "Cafe\u00e9"} + decomposed = {**SOURCE_IDENTITY, "adapter_instance_id": "Cafee\u0301"} + assert origin_identity_key(composed) == origin_identity_key(decomposed) + + internal_whitespace = {**SOURCE_IDENTITY, "adapter_instance_id": " adapter \t main "} + normalized = normalize_source_identity(internal_whitespace) + assert normalized["adapter_instance_id"] == "adapter \t main" + assert origin_identity_key(internal_whitespace) != origin_identity_key( + {**SOURCE_IDENTITY, "adapter_instance_id": "adapter main"} + ) + + zero_identity = {**SOURCE_IDENTITY, "platform_subject_id": 0} + assert normalize_source_identity(zero_identity)["platform_subject_id"] == "0" + + invalid_values = [True, 1.0, float("nan"), float("inf"), [], (), {}, "x" * 161, "ok\x00bad"] + for invalid in invalid_values: + try: + normalize_source_identity({**SOURCE_IDENTITY, "platform_subject_id": invalid}) + except ValueError as exc: + assert str(exc) == "invalid_identity" + else: + raise AssertionError(f"identity scalar must reject {invalid!r}") + + +def test_contract_errors_have_stable_statuses_and_codes(): + sample = _sample() + source = SOURCE_IDENTITY + cases = [ + (None, "quarantined", "invalid_payload"), + ({**sample, "contract_name": "wrong"}, "contract_mismatch", "invalid_contract_name"), + ({**sample, "contract_version": "2.0"}, "contract_mismatch", "unsupported_contract_version"), + ({**sample, "contract_fingerprint": "0" * 64}, "contract_mismatch", "fingerprint_mismatch"), + ({key: value for key, value in sample.items() if key != "person_id"}, "quarantined", "missing_field"), + ({**sample, "owner_mode": "surprise"}, "quarantined", "unknown_enum"), + ({**sample, "aliases": "not-a-list"}, "quarantined", "invalid_field"), + ({**sample, "projection_revision": "42"}, "quarantined", "invalid_field"), + ({**sample, "projection_revision": 0}, "quarantined", "invalid_field"), + ({**sample, "affinity_score": 12.0}, "quarantined", "invalid_field"), + ({**sample, "affinity_score": 1201}, "quarantined", "invalid_field"), + ({**sample, "affinity_band": "neutral"}, "quarantined", "invalid_field"), + ({**sample, "display_name": 12}, "quarantined", "invalid_field"), + ({**sample, "person_id": "x" * 161}, "quarantined", "invalid_field"), + ({**sample, "person_id": "person\x00bad"}, "quarantined", "invalid_field"), + ({**sample, "relation_policy_id": "x" * 161}, "quarantined", "invalid_field"), + ({**sample, "relation_label": ""}, "quarantined", "invalid_field"), + ({**sample, "display_name": "name\x00bad"}, "quarantined", "invalid_field"), + ({**sample, "updated_at": "x" * 81}, "quarantined", "invalid_field"), + ({**sample, "resolved_identity_key": "ops-origin-v1:BAD"}, "quarantined", "invalid_field"), + ({**sample, "aliases": ["x" * 121]}, "quarantined", "invalid_field"), + ({**sample, "aliases": ["alias\x00bad"]}, "quarantined", "invalid_field"), + ({**sample, "updated_at": ""}, "quarantined", "invalid_field"), + ({**sample, "confinement_until": None}, "quarantined", "invalid_field"), + ({**sample, "group_overlay_ref": 1}, "quarantined", "invalid_field"), + ({**sample, "relationship_capabilities": ["unknown"]}, "quarantined", "unknown_enum"), + ] + for payload, status, error_code in cases: + result = consume_unified_person_projection(payload, source_identity=source) + assert (result["status"], result["error_code"], result["person_id"]) == (status, error_code, "") + + +def test_bridge_unavailable_and_identity_conflict_never_infer_person_id(): + sample = _sample() + source = SOURCE_IDENTITY + unavailable = consume_unified_person_projection(sample, source_identity=source, companion_available=False) + assert (unavailable["status"], unavailable["error_code"], unavailable["person_id"]) == ( + "pending_companion", "bridge_unavailable", "", + ) + conflict = consume_unified_person_projection( + {**sample, "resolved_identity_key": "ops-origin-v1:" + "0" * 64}, source_identity=source + ) + assert (conflict["status"], conflict["error_code"], conflict["person_id"]) == ( + "identity_conflict", "identity_conflict", "", + ) + legacy = legacy_unresolved_reference(legacy_record_id="legacy-1") + assert (legacy["status"], legacy["person_id"], legacy["legacy_record_id"]) == ( + "legacy_unresolved", "", "legacy-1", + ) + + +def test_bridge_exposes_the_same_contract_probe_and_consumer(): + bridge = MemoryCompanionBridge(object()) + probe = bridge.probe_unified_person_contract() + assert probe["contract_fingerprint"] == contract_fingerprint() + assert probe["available"] is True + sample = _sample() + source = SOURCE_IDENTITY + result = bridge.consume_unified_person_projection(sample, source_identity=source) + assert result["status"] == "resolved" diff --git a/tests/test_unified_person_persistence.py b/tests/test_unified_person_persistence.py new file mode 100644 index 0000000..2dfff4c --- /dev/null +++ b/tests/test_unified_person_persistence.py @@ -0,0 +1,308 @@ +from __future__ import annotations + +import asyncio +import json +import sqlite3 +import tempfile +from contextlib import contextmanager +from pathlib import Path + +import pytest + +from core.models import EntityRef, MemoryRecord +from core.bridge import MemoryCompanionBridge +from core.store import MemoryStore +from core.unified_person_persistence import UnifiedPersonPersistence, UnifiedPersonReferenceMigration + + +FIXTURE_PATH = Path(__file__).parent / "fixtures" / "unified_person_projection_v1.json" +WORKSPACE_TEMP_ROOT = Path(__file__).resolve().parent.parent / ".tmp" +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", +} +IDENTITY_COLUMNS = tuple(SOURCE_IDENTITY) + + +@contextmanager +def _store(): + """Keep test databases inside the writable workspace on Windows.""" + WORKSPACE_TEMP_ROOT.mkdir(parents=True, exist_ok=True) + with tempfile.TemporaryDirectory(dir=WORKSPACE_TEMP_ROOT) as directory: + store = MemoryStore(Path(directory) / "memory.db") + store.initialize() + try: + yield store + finally: + store.close() + + +def _projection(**changes): + value = json.loads(FIXTURE_PATH.read_text(encoding="utf-8")) + value.update(changes) + return value + + +async def _insert(store: MemoryStore, memory_id: str, **changes) -> None: + record = MemoryRecord( + id=memory_id, + memory_type="user_fact", + memory_domain="user_memory", + subject=EntityRef(kind="user", id="42"), + scope="private", + session_id="telegram:FriendMessage:42", + platform="telegram", + visibility="private_pair", + reality_level="real_user_fact", + lifecycle="stable_memory", + content=f"fact {memory_id}", + evidence=f"evidence {memory_id}", + **changes, + ) + await store.insert_memory(record) + + +def test_schema_has_complete_identity_constraints_and_immutable_origin(): + with _store() as store: + columns = {row["name"] for row in store._conn.execute("PRAGMA table_info(memories)").fetchall()} + assert {"person_id", "origin_identity_key", "coordination_status", "projection_revision"}.issubset(columns) + source_info = store._conn.execute("PRAGMA table_info(unified_person_source_identities)").fetchall() + assert {"origin_identity_key", *IDENTITY_COLUMNS}.issubset({row["name"] for row in source_info}) + assert next(row["pk"] for row in source_info if row["name"] == "origin_identity_key") == 1 + index_names = [row["name"] for row in store._conn.execute("PRAGMA index_list(unified_person_source_identities)")] + assert any( + tuple(item["name"] for item in store._conn.execute(f"PRAGMA index_info({index_name})")) == IDENTITY_COLUMNS + for index_name in index_names + ) + queue_foreign_keys = store._conn.execute("PRAGMA foreign_key_list(unified_person_coordination_queue)").fetchall() + assert any(row["from"] == "memory_id" and row["table"] == "memories" and row["on_delete"] == "CASCADE" for row in queue_foreign_keys) + + asyncio.run(_insert(store, "m-schema")) + coordinator = UnifiedPersonPersistence(store) + result = asyncio.run(coordinator.coordinate(memory_id="m-schema", projection=_projection(), source_identity=SOURCE_IDENTITY)) + assert result["ok"] is True + with pytest.raises(sqlite3.IntegrityError, match="origin_identity_key_immutable"): + store._conn.execute("UPDATE memories SET origin_identity_key='ops-origin-v1:" + "0" * 64 + "' WHERE id='m-schema'") + + +def test_coordinate_keeps_origin_immutable_and_revision_monotonic(): + with _store() as store: + asyncio.run(_insert(store, "m-revision")) + coordinator = UnifiedPersonPersistence(store) + first = asyncio.run(coordinator.coordinate(memory_id="m-revision", projection=_projection(), source_identity=SOURCE_IDENTITY)) + assert first["ok"] is True + changed = _projection(projection_revision=2, person_id="person_after_explicit_merge") + second = asyncio.run(coordinator.coordinate(memory_id="m-revision", projection=changed, source_identity=SOURCE_IDENTITY)) + assert second["ok"] is True + stale = asyncio.run(coordinator.coordinate(memory_id="m-revision", projection=_projection(), source_identity=SOURCE_IDENTITY)) + assert (stale["ok"], stale["error_code"]) == (False, "stale_projection") + row = store._conn.execute("SELECT person_id, origin_identity_key, projection_revision, evidence FROM memories WHERE id='m-revision'").fetchone() + assert row["person_id"] == "person_after_explicit_merge" + assert row["origin_identity_key"] == first["origin_identity_key"] + assert row["projection_revision"] == 2 + assert row["evidence"] == "evidence m-revision" + + +def test_queue_is_stably_deduplicated_and_claim_replay_is_leased(): + with _store() as store: + asyncio.run(_insert(store, "m-queue")) + coordinator = UnifiedPersonPersistence(store) + first = asyncio.run(coordinator.coordinate( + memory_id="m-queue", projection=None, source_identity=SOURCE_IDENTITY, companion_available=False, operation_id="evt-1" + )) + mismatch = asyncio.run(coordinator.coordinate( + memory_id="m-queue", projection=_projection(contract_fingerprint="0" * 64), source_identity=SOURCE_IDENTITY, operation_id="evt-1" + )) + assert mismatch["queue_id"] == first["queue_id"] + assert mismatch["deduplicated"] is True + assert len(asyncio.run(coordinator.queue_rows())) == 1 + + async def claim_concurrently(): + return await asyncio.gather(coordinator.claim_pending(limit=1), coordinator.claim_pending(limit=1)) + + claimed = [item for group in asyncio.run(claim_concurrently()) for item in group] + assert len(claimed) == 1 + store._conn.execute("UPDATE unified_person_coordination_queue SET lease_until='2000-01-01T00:00:00+00:00' WHERE id=?", (first["queue_id"],)) + store._conn.commit() + expired = asyncio.run(coordinator.replay_claim( + queue_id=claimed[0]["id"], lease_token=claimed[0]["lease_token"], projection=_projection(), source_identity=SOURCE_IDENTITY + )) + assert expired["error_code"] == "lease_not_owned" + reclaimed = asyncio.run(coordinator.claim_pending(limit=1)) + assert [item["id"] for item in reclaimed] == [first["queue_id"]] + replay = asyncio.run(coordinator.replay_claim( + queue_id=reclaimed[0]["id"], lease_token=reclaimed[0]["lease_token"], projection=_projection(), source_identity=SOURCE_IDENTITY + )) + assert replay["ok"] is True + assert asyncio.run(coordinator.claim_pending(limit=1)) == [] + queue = asyncio.run(coordinator.queue_rows())[0] + assert (queue["status"], queue["lease_token"], queue["lease_until"]) == ("confirmed", "", "") + + retry = asyncio.run(coordinator.coordinate( + memory_id="m-queue", projection=None, source_identity=SOURCE_IDENTITY, companion_available=False, operation_id="evt-1" + )) + memory = store._conn.execute("SELECT person_id, coordination_status FROM memories WHERE id='m-queue'").fetchone() + assert (retry["status"], retry["error_code"], queue["status"], tuple(memory)) == ("confirmed", "", "confirmed", (_projection()["person_id"], "resolved")) + + +def test_operation_id_conflict_never_reuses_or_changes_another_queue(): + with _store() as store: + asyncio.run(_insert(store, "m-operation-one")) + asyncio.run(_insert(store, "m-operation-two", coordination_status="legacy_unresolved")) + coordinator = UnifiedPersonPersistence(store) + first = asyncio.run(coordinator.coordinate( + memory_id="m-operation-one", + projection=_projection(contract_fingerprint="0" * 64), + source_identity=SOURCE_IDENTITY, + companion_available=False, + operation_id="shared-operation", + )) + before = dict(store._conn.execute("SELECT * FROM unified_person_coordination_queue WHERE id=?", (first["queue_id"],)).fetchone()) + other_memory = asyncio.run(coordinator.coordinate( + memory_id="m-operation-two", + projection=None, + source_identity=SOURCE_IDENTITY, + companion_available=False, + operation_id="shared-operation", + )) + other_source = dict(SOURCE_IDENTITY, platform_subject_id="43") + other_origin = asyncio.run(coordinator.coordinate( + memory_id="m-operation-one", + projection=None, + source_identity=other_source, + companion_available=False, + operation_id="shared-operation", + )) + after = dict(store._conn.execute("SELECT * FROM unified_person_coordination_queue WHERE id=?", (first["queue_id"],)).fetchone()) + second_memory = store._conn.execute( + "SELECT coordination_status FROM memories WHERE id='m-operation-two'" + ).fetchone() + assert (other_memory["status"], other_memory["error_code"], other_memory["queue_id"]) == ( + "identity_conflict", "operation_id_conflict", "" + ) + assert (other_origin["status"], other_origin["error_code"], other_origin["queue_id"]) == ( + "identity_conflict", "operation_id_conflict", "" + ) + assert before == after + assert second_memory["coordination_status"] == "legacy_unresolved" + + +def test_two_independent_stores_claim_one_queue_item_once(): + with _store() as first_store: + second_store = MemoryStore(first_store.db_path) + second_store.initialize() + try: + asyncio.run(_insert(first_store, "m-two-stores")) + first = UnifiedPersonPersistence(first_store) + second = UnifiedPersonPersistence(second_store) + asyncio.run(first.coordinate( + memory_id="m-two-stores", projection=None, source_identity=SOURCE_IDENTITY, companion_available=False + )) + + async def claim_from_both(): + return await asyncio.gather(first.claim_pending(limit=1), second.claim_pending(limit=1)) + + results = asyncio.run(claim_from_both()) + claimed = [item for result in results for item in result] + assert len(claimed) == 1 + queue = first_store._conn.execute( + "SELECT attempts, lease_token, lease_until FROM unified_person_coordination_queue" + ).fetchone() + assert (queue["attempts"], bool(queue["lease_token"]), bool(queue["lease_until"])) == (1, True, True) + finally: + second_store.close() + + +def test_contract_mismatch_never_creates_person_or_source_identity_and_delete_clears_queue(): + with _store() as store: + asyncio.run(_insert(store, "m-mismatch")) + coordinator = UnifiedPersonPersistence(store) + result = asyncio.run(coordinator.coordinate( + memory_id="m-mismatch", projection=_projection(contract_fingerprint="0" * 64), source_identity=SOURCE_IDENTITY + )) + assert (result["status"], result["person_id"]) == ("contract_mismatch", "") + memory = store._conn.execute("SELECT person_id, origin_identity_key, coordination_status FROM memories WHERE id='m-mismatch'").fetchone() + assert tuple(memory) == ("", "", "contract_mismatch") + assert store._conn.execute("SELECT COUNT(*) FROM unified_person_source_identities").fetchone()[0] == 0 + assert asyncio.run(store.delete_memory("m-mismatch")) is True + assert store._conn.execute("SELECT COUNT(*) FROM unified_person_coordination_queue").fetchone()[0] == 0 + + +def test_reference_migration_is_previewable_transactional_idempotent_and_rollbackable(): + with _store() as store: + asyncio.run(_insert(store, "m-pending")) + asyncio.run(_insert( + store, + "m-legacy", + person_id="legacy-person-reference", + coordination_status="legacy_unresolved", + projection_revision=4, + )) + migration = UnifiedPersonReferenceMigration(store) + preview = asyncio.run(migration.preview()) + assert (preview["readonly"], preview["would_mark_legacy_unresolved"]) == (True, 1) + assert preview["items"][0]["id"] == "m-legacy" + interrupted = asyncio.run(migration.apply(migration_id="p1-interrupt", fault_after=1)) + assert (interrupted["ok"], interrupted["error_code"]) == (False, "migration_interrupted") + assert store._conn.execute("SELECT COUNT(*) FROM unified_person_migration_backup WHERE migration_id='p1-interrupt'").fetchone()[0] == 0 + assert store._conn.execute("SELECT COUNT(*) FROM unified_person_migration_runs WHERE migration_id='p1-interrupt'").fetchone()[0] == 0 + applied = asyncio.run(migration.apply(migration_id="p1-complete")) + assert applied["ok"] is True and Path(applied["backup_path"]).is_file() + assert store._conn.execute("SELECT COUNT(*) FROM unified_person_migration_backup WHERE migration_id='p1-complete'").fetchone()[0] == 1 + backups_after_apply = {path.name for path in store.db_path.parent.glob("memory.backup.*.before_unified_person_p1.*.db")} + again = asyncio.run(migration.apply(migration_id="p1-complete")) + assert again["idempotent"] is True + assert {path.name for path in store.db_path.parent.glob("memory.backup.*.before_unified_person_p1.*.db")} == backups_after_apply + store._conn.execute("UPDATE memories SET person_id='', coordination_status='pending_companion', projection_revision=0 WHERE id='m-legacy'") + store._conn.commit() + rolled = asyncio.run(migration.rollback("p1-complete")) + restored = store._conn.execute("SELECT person_id, coordination_status, projection_revision FROM memories WHERE id='m-legacy'").fetchone() + assert rolled["restored"] == 1 + assert tuple(restored) == ("legacy-person-reference", "legacy_unresolved", 4) + assert store._conn.execute("SELECT 1 FROM sqlite_master WHERE type='table' AND name='unified_person_coordination_queue'").fetchone() + + +def test_persistence_bridge_exposes_all_advertised_wrappers_and_missing_results(): + class Plugin: + async def bridge_coordinate_unified_person_reference(self, **kwargs): + return {"ok": True, "status": "resolved", "error_code": "", "person_id": "person-1"} + + async def bridge_claim_unified_person_coordination(self, *, limit): + return [{"id": "queue-1", "limit": limit}] + + async def bridge_replay_unified_person_coordination(self, **kwargs): + return {"ok": True, "status": "resolved", "error_code": "", "person_id": "person-1"} + + async def preview_unified_person_reference_migration(self, *, limit): + return {"mode": "preview", "ok": True, "limit": limit, "items": []} + + async def apply_unified_person_reference_migration(self, *, migration_id, limit): + return {"mode": "apply", "ok": True, "migration_id": migration_id, "limit": limit} + + async def rollback_unified_person_reference_migration(self, migration_id): + return {"mode": "rollback", "ok": True, "migration_id": migration_id} + + bridge = MemoryCompanionBridge(Plugin()) + assert bridge.probe_unified_person_persistence_capabilities()["available"] is True + assert asyncio.run(bridge.claim_unified_person_coordination(limit=3))["items"] == [{"id": "queue-1", "limit": 3}] + assert asyncio.run(bridge.replay_unified_person_coordination( + queue_id="queue-1", lease_token="lease", projection=None, source_identity=None + ))["ok"] is True + assert asyncio.run(bridge.preview_unified_person_reference_migration(limit=2))["ok"] is True + assert asyncio.run(bridge.apply_unified_person_reference_migration(migration_id="p1", limit=2))["ok"] is True + assert asyncio.run(bridge.rollback_unified_person_reference_migration("p1"))["ok"] is True + + missing = MemoryCompanionBridge(object()) + capability = missing.probe_unified_person_persistence_capabilities() + assert capability["available"] is False + assert all(value is False for value in capability["operation_availability"].values()) + assert asyncio.run(missing.claim_unified_person_coordination())["error_code"] == "bridge_method_unavailable" + assert asyncio.run(missing.replay_unified_person_coordination( + queue_id="queue-1", lease_token="lease", projection=None, source_identity=None + ))["error_code"] == "bridge_method_unavailable" + assert asyncio.run(missing.preview_unified_person_reference_migration())["error_code"] == "bridge_method_unavailable" + assert asyncio.run(missing.apply_unified_person_reference_migration(migration_id="p1"))["error_code"] == "bridge_method_unavailable" + assert asyncio.run(missing.rollback_unified_person_reference_migration("p1"))["error_code"] == "bridge_method_unavailable"