feat(c3): add structured Bot Personal archive bridge

这个提交存在于:
Codex Local 2026-07-30 04:23:49 +08:00
父节点 f35e55c456
当前提交 5ca70d45bd
共有 5 个文件被更改,包括 790 次插入 和 0 次删除

查看文件

@ -0,0 +1,81 @@
from __future__ import annotations
"""Failure-contained consumer for the optional Bot Personal Bridge."""
from copy import deepcopy
from typing import Any
from .bot_personal_dto import BotPersonalArchiveDTO, BotPersonalValidationError, build_bot_personal_archive
def _base(*, state: str = "retry", attempts: int = 0, error_code: str | None = None) -> dict[str, Any]:
return {
"ok": False,
"record_id": "",
"deduplicated": False,
"version": 0,
"error_code": error_code,
"state": state,
"attempts": max(0, int(attempts or 0)),
}
class BotPersonalConsumer:
def __init__(self, bridge: Any, *, max_attempts: int = 3) -> None:
self.bridge = bridge
self.max_attempts = max(1, int(max_attempts or 3))
async def consume_bot_personal_archive(
self,
envelope: BotPersonalArchiveDTO | dict[str, Any],
*,
attempt: int = 1,
max_attempts: int | None = None,
) -> dict[str, Any]:
attempts = max(1, int(attempt or 1))
ceiling = max(1, int(max_attempts or self.max_attempts))
try:
dto = build_bot_personal_archive(envelope)
except BotPersonalValidationError as exc:
return {**_base(state="invalid", attempts=attempts, error_code=exc.error_code), "field": exc.field}
sender = getattr(self.bridge, "record_bot_personal_archive", None)
if not callable(sender):
return _base(state="dead_letter" if attempts >= ceiling else "retry", attempts=attempts, error_code="bridge_method_unavailable")
try:
result = await sender(dto)
except Exception:
return _base(state="dead_letter" if attempts >= ceiling else "retry", attempts=attempts, error_code="bridge_exception")
if not isinstance(result, dict):
return _base(state="dead_letter" if attempts >= ceiling else "retry", attempts=attempts, error_code="invalid_bridge_response")
normalized = _base(
state=str(result.get("state") or ("deduplicated" if result.get("deduplicated") else "sent" if result.get("ok") else "retry")),
attempts=attempts,
error_code=result.get("error_code"),
)
normalized.update({key: result[key] for key in ("record_id", "deduplicated", "version", "field") if key in result})
normalized["ok"] = bool(result.get("ok"))
if normalized["ok"]:
normalized["state"] = "deduplicated" if normalized["deduplicated"] else "sent"
return normalized
if normalized["state"] not in {"invalid", "version_conflict", "stale_version", "dead_letter"}:
normalized["state"] = "dead_letter" if attempts >= ceiling else "retry"
return normalized
async def consume(self, envelope: BotPersonalArchiveDTO | dict[str, Any], **kwargs: Any) -> dict[str, Any]:
return await self.consume_bot_personal_archive(envelope, **kwargs)
async def consume_bot_personal_archive(
bridge: Any,
envelope: BotPersonalArchiveDTO | dict[str, Any],
*,
attempt: int = 1,
max_attempts: int = 3,
) -> dict[str, Any]:
return await BotPersonalConsumer(bridge, max_attempts=max_attempts).consume_bot_personal_archive(
deepcopy(envelope) if isinstance(envelope, dict) else envelope,
attempt=attempt,
)
__all__ = ["BotPersonalConsumer", "consume_bot_personal_archive"]

308
core/bot_personal_dto.py 普通文件
查看文件

@ -0,0 +1,308 @@
from __future__ import annotations
"""Structured, privacy-limited DTOs for the Bot Personal archive boundary."""
from copy import deepcopy
from dataclasses import dataclass
from datetime import datetime, timezone
import hashlib
import json
import re
from typing import Any, Mapping
from . import bot_personal_contract
from .bot_personal_contract import (
BOT_PERSONAL_MEMORY_DOMAIN,
BOT_PERSONAL_MEMORY_TYPES,
BOT_PERSONAL_MAX_PAYLOAD_BYTES,
BOT_PERSONAL_PAYLOAD_SCHEMA_VERSION,
BOT_PERSONAL_SUBJECT,
BOT_PERSONAL_WINDOWS,
TYPE_CONTRACTS,
WINDOW_SLUGS,
)
class BotPersonalValidationError(ValueError):
def __init__(self, error_code: str, field: str = "", message: str = "") -> None:
self.error_code = str(error_code or "invalid").strip()[:80]
self.field = str(field or "").strip()[:160]
self.message = str(message or self.error_code).strip()[:300]
super().__init__(self.message)
_FORBIDDEN_KEY_PARTS = (
"prompt", "conversation", "transcript", "message_chain", "message_history", "raw_message",
"messages",
"raw_prompt", "chat_history", "cookie", "token", "secret", "credential", "password",
"passwd", "authorization", "bearer", "api_key", "apikey", "private_key", "base64",
"binary", "media_bytes", "image_bytes", "audio_bytes", "video_bytes", "media_binary",
"media_data", "media_content", "media_blob", "file_path", "filepath", "absolute_path",
"local_path", "raw_payload",
)
_SENSITIVE_VALUE_PATTERNS = (
re.compile(r"(?i)^bearer\s+\S+"),
re.compile(r"(?i)(?:password|passwd|secret|token|api[_-]?key|authorization)\s*[:=]\s*\S+"),
re.compile(r"(?i)base64,"),
re.compile(r"(?i)^data:[a-z0-9.+-]+/[a-z0-9.+-]+[;,]"),
re.compile(r"(?i)^-----begin "),
re.compile(r"(?i)(?:^|[\s=:;,])(?:[A-Za-z]:[\\/]|\\\\|/home/|/root/|/tmp/|/var/|/volume\d+/)"),
)
_FORBIDDEN_TOP_LEVEL_KEYS = frozenset({
"conversation", "transcript", "message_chain", "raw_prompt", "raw_payload", "payload_raw",
})
def _key_name(value: Any) -> str:
return str(value or "").strip().lower().replace("-", "_")
def _text(value: Any, limit: int) -> str:
return str(value or "").strip()[:limit]
def _check_string(value: str, path: str) -> str:
text = _text(value, BOT_PERSONAL_MAX_PAYLOAD_BYTES)
if any(pattern.search(text) for pattern in _SENSITIVE_VALUE_PATTERNS):
raise BotPersonalValidationError("privacy_rejected", path, "credential, binary media or absolute path is not accepted")
return text
def sanitize_bot_personal_value(value: Any, *, path: str = "payload", depth: int = 0) -> Any:
if depth > 8:
raise BotPersonalValidationError("privacy_rejected", path, "payload nesting is too deep")
if isinstance(value, Mapping):
if len(value) > 64:
raise BotPersonalValidationError("payload_too_large", path, "too many fields")
result: dict[str, Any] = {}
for raw_key, raw_value in value.items():
key = _text(raw_key, 80)
normalized = _key_name(key)
if not key:
raise BotPersonalValidationError("invalid", path, "empty field name")
if any(part in normalized for part in _FORBIDDEN_KEY_PARTS):
raise BotPersonalValidationError("privacy_rejected", f"{path}.{key}", "sensitive or raw field is not accepted")
result[key] = sanitize_bot_personal_value(raw_value, path=f"{path}.{key}", depth=depth + 1)
return result
if isinstance(value, (bytes, bytearray, memoryview)):
raise BotPersonalValidationError("privacy_rejected", path, "binary media is not accepted")
if isinstance(value, (list, tuple)):
if len(value) > 64:
raise BotPersonalValidationError("payload_too_large", path, "too many list items")
return [sanitize_bot_personal_value(item, path=f"{path}[{index}]", depth=depth + 1) for index, item in enumerate(value)]
if isinstance(value, str):
return _check_string(value, path)
if value is None or isinstance(value, (bool, int, float)):
return value
raise BotPersonalValidationError("invalid", path, "value is not JSON serializable")
def _json_size(value: Any) -> int:
try:
return len(json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":")).encode("utf-8"))
except (TypeError, ValueError, OverflowError) as exc:
raise BotPersonalValidationError("invalid", "payload", f"payload is not JSON serializable: {exc}") from exc
def _parse_datetime(value: str, field: str) -> datetime:
try:
parsed = datetime.fromisoformat(value.replace("Z", "+00:00"))
except (TypeError, ValueError) as exc:
raise BotPersonalValidationError("invalid", field, f"{field} must be ISO-8601") from exc
if parsed.tzinfo is None or parsed.utcoffset() is None:
raise BotPersonalValidationError("invalid", field, f"{field} requires a timezone")
return parsed
def _derive_window(occurred_at: str) -> str:
hour = _parse_datetime(occurred_at, "occurred_at").hour
minutes = hour * 60 + _parse_datetime(occurred_at, "occurred_at").minute
return bot_personal_contract.window_for_minutes(minutes)
def _record_id(memory_type: str, idempotency_key: str) -> str:
digest = hashlib.sha256(f"{BOT_PERSONAL_MEMORY_DOMAIN}|{memory_type}|{idempotency_key}".encode("utf-8")).hexdigest()[:24]
return f"botmem_{digest}"
def _fingerprint(data: Mapping[str, Any]) -> str:
canonical = json.dumps(dict(data), ensure_ascii=False, sort_keys=True, separators=(",", ":"))
return hashlib.sha256(canonical.encode("utf-8")).hexdigest()
def validate_bot_personal_idempotency_key(value: Any) -> str:
key = _text(value, 240)
if not key or any(ord(char) < 32 for char in key):
raise BotPersonalValidationError("invalid", "idempotency_key", "idempotency_key is required and must be printable")
normalized = _key_name(key)
if any(part in normalized for part in _FORBIDDEN_KEY_PARTS) or any(pattern.search(key) for pattern in _SENSITIVE_VALUE_PATTERNS):
raise BotPersonalValidationError("privacy_rejected", "idempotency_key", "unsafe idempotency_key")
return key
@dataclass(frozen=True)
class BotPersonalArchiveDTO:
record_id: str
memory_domain: str
memory_type: str
subject: str
date: str
window: str
occurred_at: str
source_kind: str
source_refs: tuple[str, ...]
certainty: float
evidence_level: str
status: str
version: int
idempotency_key: str
payload_schema_version: str
payload: dict[str, Any]
def envelope(self) -> dict[str, Any]:
return {
"record_id": self.record_id,
"memory_domain": self.memory_domain,
"memory_type": self.memory_type,
"subject": self.subject,
"date": self.date,
"window": self.window,
"occurred_at": self.occurred_at,
"source_kind": self.source_kind,
"source_refs": list(self.source_refs),
"certainty": self.certainty,
"evidence_level": self.evidence_level,
"status": self.status,
"version": self.version,
"idempotency_key": self.idempotency_key,
"payload_schema_version": self.payload_schema_version,
"payload": deepcopy(self.payload),
}
to_dict = envelope
def build_bot_personal_archive(
envelope: BotPersonalArchiveDTO | Mapping[str, Any] | None = None,
*,
memory_type: str | None = None,
payload: Mapping[str, Any] | None = None,
record_id: str = "",
memory_domain: str | None = None,
subject: str | None = None,
date: str = "",
window: str = "",
occurred_at: str = "",
source_kind: str = "",
source_refs: Any = None,
certainty: Any = None,
evidence_level: str = "",
status: str = "",
version: Any = None,
idempotency_key: str = "",
payload_schema_version: str | None = None,
) -> BotPersonalArchiveDTO:
if isinstance(envelope, BotPersonalArchiveDTO):
return envelope
source: dict[str, Any] = dict(envelope or {}) if isinstance(envelope, Mapping) else {}
if envelope is not None and not isinstance(envelope, (BotPersonalArchiveDTO, Mapping)):
raise BotPersonalValidationError("invalid", "envelope", "envelope must be an object")
supplied = {key: value for key, value in {
"memory_type": memory_type, "payload": payload, "record_id": record_id, "memory_domain": memory_domain,
"subject": subject, "date": date, "window": window, "occurred_at": occurred_at, "source_kind": source_kind,
"source_refs": source_refs, "certainty": certainty, "evidence_level": evidence_level, "status": status,
"version": version, "idempotency_key": idempotency_key, "payload_schema_version": payload_schema_version,
}.items() if value not in (None, "")}
source.update(supplied)
for key in source:
if _key_name(key) in _FORBIDDEN_TOP_LEVEL_KEYS or any(part in _key_name(key) for part in _FORBIDDEN_KEY_PARTS):
raise BotPersonalValidationError("privacy_rejected", str(key), "sensitive or raw field is not accepted")
kind = _text(source.get("memory_type"), 80).lower()
if kind not in BOT_PERSONAL_MEMORY_TYPES or kind not in TYPE_CONTRACTS:
raise BotPersonalValidationError("invalid", "memory_type", "unknown Bot Personal memory type")
if _text(source.get("memory_domain"), 80) not in ("", BOT_PERSONAL_MEMORY_DOMAIN):
raise BotPersonalValidationError("invalid", "memory_domain", "Bot Personal domain is fixed")
if _text(source.get("subject"), 80) not in ("", BOT_PERSONAL_SUBJECT):
raise BotPersonalValidationError("invalid", "subject", "Bot Personal subject is fixed")
key = validate_bot_personal_idempotency_key(source.get("idempotency_key"))
raw_payload = source.get("payload")
if not isinstance(raw_payload, Mapping):
raise BotPersonalValidationError("invalid", "payload", "payload must be an object")
safe_payload = sanitize_bot_personal_value(raw_payload, path="payload")
if _json_size(safe_payload) > BOT_PERSONAL_MAX_PAYLOAD_BYTES:
raise BotPersonalValidationError("payload_too_large", "payload", "payload exceeds the contract limit")
occurred = _text(source.get("occurred_at"), 80)
if not occurred:
occurred = datetime.now(timezone.utc).isoformat(timespec="seconds")
_parse_datetime(occurred, "occurred_at")
record_date = _text(source.get("date"), 20) or occurred[:10]
try:
datetime.strptime(record_date, "%Y-%m-%d")
except ValueError as exc:
raise BotPersonalValidationError("invalid", "date", "date must be YYYY-MM-DD") from exc
record_window = _text(source.get("window"), 40).lower() or _derive_window(occurred)
if record_window not in BOT_PERSONAL_WINDOWS or record_window not in WINDOW_SLUGS:
raise BotPersonalValidationError("invalid", "window", "window is outside the shared contract")
try:
confidence = float(source.get("certainty", 0.6))
except (TypeError, ValueError) as exc:
raise BotPersonalValidationError("invalid", "certainty", "certainty must be numeric") from exc
if not 0.0 <= confidence <= 1.0:
raise BotPersonalValidationError("invalid", "certainty", "certainty must be between 0 and 1")
try:
record_version = int(source.get("version", 1))
except (TypeError, ValueError) as exc:
raise BotPersonalValidationError("invalid", "version", "version must be a positive integer") from exc
if record_version < 1:
raise BotPersonalValidationError("invalid", "version", "version must be a positive integer")
schema = _text(source.get("payload_schema_version"), 40) or BOT_PERSONAL_PAYLOAD_SCHEMA_VERSION
if schema != BOT_PERSONAL_PAYLOAD_SCHEMA_VERSION:
raise BotPersonalValidationError("invalid", "payload_schema_version", "unsupported payload schema")
refs_value = source.get("source_refs")
if refs_value is None:
refs_value = [f"archive:{key}"]
if not isinstance(refs_value, (list, tuple)):
raise BotPersonalValidationError("invalid", "source_refs", "source_refs must be a list")
refs = tuple(_check_string(_text(item, 240), "source_refs") for item in refs_value if _text(item, 240))
if not refs:
raise BotPersonalValidationError("invalid", "source_refs", "source_refs must not be empty")
contract_source, contract_evidence, contract_status = TYPE_CONTRACTS[kind]
normalized = {
"memory_type": kind, "idempotency_key": key, "version": record_version, "payload": safe_payload,
"date": record_date, "window": record_window, "occurred_at": occurred, "source_refs": list(refs),
"certainty": confidence, "source_kind": contract_source, "evidence_level": contract_evidence,
"status": contract_status,
}
return BotPersonalArchiveDTO(
record_id=_record_id(kind, key),
memory_domain=BOT_PERSONAL_MEMORY_DOMAIN,
memory_type=kind,
subject=BOT_PERSONAL_SUBJECT,
date=record_date,
window=record_window,
occurred_at=occurred,
source_kind=contract_source,
source_refs=refs,
certainty=confidence,
evidence_level=contract_evidence,
status=contract_status,
version=record_version,
idempotency_key=key,
payload_schema_version=schema,
payload=deepcopy(safe_payload),
)
def bot_personal_payload_fingerprint(dto: BotPersonalArchiveDTO) -> str:
payload = dto.envelope()
payload.pop("record_id", None)
return _fingerprint(payload)
__all__ = [
"BotPersonalArchiveDTO", "BotPersonalValidationError", "build_bot_personal_archive",
"bot_personal_payload_fingerprint", "sanitize_bot_personal_value", "validate_bot_personal_idempotency_key",
]

查看文件

@ -5,6 +5,7 @@ from typing import Any
from zoneinfo import ZoneInfo
from . import bot_personal_contract
from .bot_personal_dto import BotPersonalArchiveDTO, build_bot_personal_archive
from .context_consumer import consume_context_projection
from .models import EntityRef, MemoryRecord, SessionContext, clean_text
from .person_projection import consume_person_projection
@ -229,6 +230,91 @@ class MemoryCompanionBridge:
kwargs.setdefault("importance", 0.45)
return await self.record_event(content=content, **kwargs)
async def record_bot_personal_archive(self, envelope: BotPersonalArchiveDTO | dict[str, Any]) -> dict[str, Any]:
"""Send one validated Bot Personal archive envelope without leaking failures."""
base = {
"ok": False,
"record_id": "",
"deduplicated": False,
"version": 0,
"error_code": None,
"state": "degraded",
}
try:
dto = build_bot_personal_archive(envelope)
except Exception as exc:
return {**base, "state": "invalid", "error_code": getattr(exc, "error_code", "invalid")}
try:
recorder = getattr(self._plugin, "record_bot_personal_archive", None)
except Exception:
recorder = None
if not callable(recorder):
return {**base, "error_code": "bridge_method_unavailable", "state": "degraded"}
try:
result = await recorder(dto)
except Exception:
return {**base, "error_code": "bridge_exception", "state": "degraded"}
if not isinstance(result, dict):
return {**base, "error_code": "invalid_bridge_response", "state": "degraded"}
normalized = dict(base)
for key in base:
if key in result:
normalized[key] = result[key]
normalized["ok"] = bool(result.get("ok"))
normalized["deduplicated"] = bool(result.get("deduplicated"))
normalized["version"] = int(result.get("version") or 0)
if normalized["ok"]:
normalized["state"] = "deduplicated" if normalized["deduplicated"] else "sent"
elif normalized["state"] == "ready":
normalized["state"] = "degraded"
return normalized
async def record_bot_personal_memory(self, *, memory_type: str, payload: dict[str, Any] | None = None, **kwargs: Any) -> dict[str, Any]:
"""Compatibility alias that still crosses the structured archive boundary."""
try:
envelope = build_bot_personal_archive(memory_type=memory_type, payload=payload or {}, **kwargs)
except Exception as exc:
return {
"ok": False, "record_id": "", "deduplicated": False, "version": 0,
"error_code": getattr(exc, "error_code", "invalid"), "state": "invalid",
}
return await self.record_bot_personal_archive(envelope)
async def read_bot_personal_profile(self, query: str = "", *, limit: int = 10) -> dict[str, Any]:
"""Read only safe Bot Personal summaries; never return archive payloads."""
base = {"ok": False, "read_only": True, "state": "degraded", "degraded": True, "pending": True, "items": []}
try:
getter = getattr(self._plugin, "read_bot_personal_profile", None)
except Exception:
getter = None
if not callable(getter):
return {**base, "error_code": "bridge_method_unavailable"}
try:
result = await getter(query=query, limit=limit)
except Exception:
return {**base, "error_code": "bridge_exception"}
if not isinstance(result, dict):
return {**base, "error_code": "invalid_bridge_response"}
safe_keys = {
"record_id", "memory_type", "memory_domain", "subject", "date", "window", "occurred_at",
"source_kind", "source_refs", "evidence_level", "status", "version", "summary", "reference",
}
items = result.get("items", result.get("memories", []))
safe_items: list[dict[str, Any]] = []
for item in items if isinstance(items, list) else []:
if not isinstance(item, dict):
continue
safe_items.append({key: item[key] for key in safe_keys if key in item and key not in {"payload", "content"}})
return {
"ok": bool(result.get("ok", True)), "read_only": True,
"state": "ready" if result.get("state") in (None, "ready") else result.get("state"),
"degraded": bool(result.get("degraded", False)), "pending": bool(result.get("pending", False)),
"items": safe_items,
}
async def search_bot_personal_profile(self, query: str = "", *, limit: int = 10) -> dict[str, Any]:
return await self.read_bot_personal_profile(query=query, limit=limit)
async def search(
self,
query: str,

查看文件

@ -23,6 +23,12 @@ from .astrbot_compat import (
sanitize_request_history,
)
from .bridge import serialize_memory
from .bot_personal_dto import (
BotPersonalArchiveDTO,
BotPersonalValidationError,
bot_personal_payload_fingerprint,
build_bot_personal_archive,
)
from .chat_import import HistoricalChatImporter
from .classifier import MemoryClassifier
from .config import ConfigView
@ -656,6 +662,176 @@ class MemoryCompanionService:
)
return memory_id
async def record_bot_personal_archive(self, envelope: BotPersonalArchiveDTO | dict[str, Any]) -> dict[str, Any]:
"""Persist one Bot Personal envelope in its isolated memory domain."""
result = {
"ok": False, "record_id": "", "deduplicated": False, "version": 0,
"error_code": None, "state": "degraded",
}
try:
dto = build_bot_personal_archive(envelope)
except BotPersonalValidationError as exc:
return {**result, "state": "invalid", "error_code": exc.error_code, "field": exc.field}
except Exception:
return {**result, "state": "invalid", "error_code": "invalid"}
store = getattr(self, "store", None)
getter = getattr(store, "get_memory", None)
inserter = getattr(store, "insert_memory", None)
if not callable(inserter):
return {**result, "error_code": "store_unavailable", "state": "degraded"}
fingerprint = bot_personal_payload_fingerprint(dto)
existing = None
if callable(getter):
try:
existing = await getter(dto.record_id)
except Exception:
return {**result, "record_id": dto.record_id, "error_code": "store_unavailable", "state": "degraded"}
if existing is not None:
metadata = existing.metadata if isinstance(existing.metadata, dict) else {}
old_version = int(metadata.get("version") or 0)
old_fingerprint = str(metadata.get("payload_fingerprint") or "")
if old_version > dto.version:
return {**result, "record_id": dto.record_id, "version": old_version, "error_code": "stale_version", "state": "stale_version"}
if old_version == dto.version:
if old_fingerprint == fingerprint:
return {**result, "ok": True, "record_id": dto.record_id, "deduplicated": True, "version": old_version, "state": "deduplicated"}
return {**result, "record_id": dto.record_id, "version": old_version, "error_code": "version_conflict", "state": "version_conflict"}
payload = dict(dto.payload)
summary = clean_text(payload.get("summary") or payload.get("title") or payload.get("activity") or dto.memory_type, 360)
# The raw payload stays in the isolated Bot domain for future internal use;
# Profile readers below deliberately expose only a safe reference.
metadata = {
"bot_personal": True,
"memory_domain": dto.memory_domain,
"subject": dto.subject,
"date": dto.date,
"window": dto.window,
"occurred_at": dto.occurred_at,
"source_kind": dto.source_kind,
"source_refs": list(dto.source_refs),
"certainty": dto.certainty,
"evidence_level": dto.evidence_level,
"status": dto.status,
"version": dto.version,
"idempotency_key": dto.idempotency_key,
"payload_schema_version": dto.payload_schema_version,
"payload_fingerprint": fingerprint,
"payload": payload,
}
reality_level = {
"planned": "planned",
"observed": "observed_activity",
"reconciled": "reconciled",
"projection": "window_projection",
"subjective": "subjective",
"creative": "creative_work",
"media": "media_index",
"shared": "shared_activity",
"detail": "detail_fragment",
"calendar": "calendar_event",
"proactive": "proactive_action",
}.get(dto.source_kind, dto.source_kind)
record = MemoryRecord(
id=dto.record_id,
memory_type=dto.memory_type,
subject=EntityRef(kind="bot", id=dto.subject, role="subject"),
object=EntityRef(kind="bot", id=dto.subject, role="object"),
scope="private",
session_id="bot_personal",
visibility="bot_self",
sayability="indirect",
reality_level=reality_level,
lifecycle="planned_projection" if dto.status == "planned" else "stable_memory",
content=f"Bot Personal archive reference [{dto.memory_type}]",
evidence="; ".join(dto.source_refs),
confidence=dto.certainty,
importance=0.55,
review_status="auto",
tags=["bot_personal", dto.memory_type, dto.source_kind],
metadata=metadata,
source_plugin="bot_personal_bridge",
occurred_at=dto.occurred_at,
# MemoryStore's generic content dedupe intentionally ignores the
# metadata payload. Bot Personal records therefore need a stable
# key-derived fingerprint or different agenda keys would merge
# into one row merely because their display reference is similar.
content_fingerprint=hashlib.sha256(
f"bot_personal|{dto.memory_type}|{dto.idempotency_key}".encode("utf-8")
).hexdigest(),
)
try:
memory_id = await inserter(record)
scheduler = getattr(self, "_schedule_memory_embedding", None)
if callable(scheduler):
try:
scheduler(memory_id, record)
except Exception:
pass
except Exception:
return {**result, "record_id": dto.record_id, "version": dto.version, "error_code": "store_write_failed", "state": "degraded"}
return {**result, "ok": True, "record_id": dto.record_id, "version": dto.version, "state": "sent"}
async def record_bot_personal_memory(self, *, memory_type: str, payload: dict[str, Any] | None = None, **kwargs: Any) -> dict[str, Any]:
try:
dto = build_bot_personal_archive(memory_type=memory_type, payload=payload or {}, **kwargs)
except BotPersonalValidationError as exc:
return {"ok": False, "record_id": "", "deduplicated": False, "version": 0, "error_code": exc.error_code, "state": "invalid"}
return await self.record_bot_personal_archive(dto)
async def read_bot_personal_profile(self, query: str = "", *, limit: int = 10) -> dict[str, Any]:
"""Read-only, domain-isolated Bot Personal references with no payload output."""
store = getattr(self, "store", None)
lister = getattr(store, "list_memories", None)
if not callable(lister):
return {"ok": False, "read_only": True, "state": "degraded", "degraded": True, "pending": True, "items": [], "error_code": "store_unavailable"}
try:
records = await lister(
limit=max(1, min(100, int(limit or 10) * 3)),
include_pending=False,
query="",
scope="private",
visibility="bot_self",
session_id="bot_personal",
)
except Exception:
return {"ok": False, "read_only": True, "state": "degraded", "degraded": True, "pending": True, "items": [], "error_code": "store_unavailable"}
query_text = clean_text(query, 240).lower()
items: list[dict[str, Any]] = []
for record in records:
metadata = record.metadata if isinstance(record.metadata, dict) else {}
if clean_text(metadata.get("memory_domain"), 80) != "bot_self_schedule":
continue
searchable = " ".join([
clean_text(record.memory_type, 80), clean_text(metadata.get("date"), 20),
clean_text(metadata.get("window"), 40), clean_text(metadata.get("source_kind"), 40),
clean_text((metadata.get("payload") or {}).get("summary"), 240)
if isinstance(metadata.get("payload"), dict) else "",
clean_text((metadata.get("payload") or {}).get("title"), 240)
if isinstance(metadata.get("payload"), dict) else "",
clean_text((metadata.get("payload") or {}).get("activity"), 240)
if isinstance(metadata.get("payload"), dict) else "",
]).lower()
if query_text and query_text not in searchable:
continue
items.append({
"record_id": record.id,
"memory_domain": "bot_self_schedule",
"memory_type": record.memory_type,
"subject": "bot_self",
"date": clean_text(metadata.get("date"), 20),
"window": clean_text(metadata.get("window"), 40),
"occurred_at": clean_text(record.occurred_at, 80),
"source_kind": clean_text(metadata.get("source_kind"), 40),
"source_refs": [clean_text(item, 240) for item in metadata.get("source_refs", []) if clean_text(item, 240)][:8],
"evidence_level": clean_text(metadata.get("evidence_level"), 8),
"status": clean_text(metadata.get("status"), 40),
"version": int(metadata.get("version") or 0),
"summary": f"Bot Personal archive reference [{record.memory_type}]",
})
return {"ok": True, "read_only": True, "state": "ready", "degraded": False, "pending": False, "items": items[:max(1, min(100, int(limit or 10)))]}
async def bridge_search(
self,
query: str,

查看文件

@ -0,0 +1,139 @@
from __future__ import annotations
import asyncio
from pathlib import Path
import tempfile
from core.bot_personal_consumer import BotPersonalConsumer
from core.bot_personal_contract import BOT_PERSONAL_MEMORY_DOMAIN, BOT_PERSONAL_MEMORY_TYPES, TYPE_CONTRACTS, WINDOW_SLUGS
from core.bot_personal_dto import BotPersonalValidationError, build_bot_personal_archive
from core.bridge import MemoryCompanionBridge
from core.models import MemoryRecord
from core.service import MemoryCompanionService
from core.store import MemoryStore
def envelope(index: str = "1", **overrides):
value = {
"memory_type": "bot_observed_activity",
"memory_domain": BOT_PERSONAL_MEMORY_DOMAIN,
"subject": "bot_self",
"date": "2026-07-30",
"window": "afternoon",
"occurred_at": "2026-07-30T15:00:00+08:00",
"source_kind": "wrong_input_is_corrected",
"source_refs": [f"companion:event:{index}"],
"certainty": 0.9,
"evidence_level": "L0",
"status": "wrong_input_is_corrected",
"version": 1,
"idempotency_key": f"c3:test:{index}",
"payload_schema_version": "1.0",
"payload": {"summary": "safe archive summary", "activity": "rest"},
}
value.update(overrides)
return value
def make_service(tmp_path: Path):
service = object.__new__(MemoryCompanionService)
service.store = MemoryStore(tmp_path / "memory.db")
service.store.initialize()
service._schedule_memory_embedding = lambda *args, **kwargs: None
return service
def run(coro):
return asyncio.run(coro)
def test_contract_windows_and_types_are_imported_from_single_contract():
dto = build_bot_personal_archive(envelope())
assert tuple(WINDOW_SLUGS) == ("late_night", "morning", "noon", "afternoon", "evening")
assert set(BOT_PERSONAL_MEMORY_TYPES) == set(TYPE_CONTRACTS)
assert dto.window in WINDOW_SLUGS
assert dto.source_kind == TYPE_CONTRACTS[dto.memory_type][0]
assert dto.evidence_level == TYPE_CONTRACTS[dto.memory_type][1]
def test_privacy_rejects_raw_prompt_credentials_binary_path_and_unsafe_key():
for update in (
{"payload": {"raw_prompt": "do not accept"}},
{"payload": {"auth_token": "secret-value"}},
{"payload": {"media_bytes": b"binary"}},
{"payload": {"photo": "C:\\private\\photo.png"}},
{"idempotency_key": "photo:C:\\private\\photo.png"},
):
try:
build_bot_personal_archive({**envelope(), **update})
except BotPersonalValidationError as exc:
assert exc.error_code == "privacy_rejected"
else:
raise AssertionError("privacy input was accepted")
def test_service_success_idempotency_version_conflict_and_stale(tmp_path):
service = make_service(tmp_path)
try:
first = run(service.record_bot_personal_archive(envelope("same")))
duplicate = run(service.record_bot_personal_archive(envelope("same")))
conflict = run(service.record_bot_personal_archive(envelope("same", payload={"summary": "changed"})))
stale = run(service.record_bot_personal_archive(envelope("same", version=0)))
newer = run(service.record_bot_personal_archive(envelope("same", version=2, payload={"summary": "new"})))
assert first["ok"] and first["state"] == "sent"
assert duplicate["ok"] and duplicate["deduplicated"] and duplicate["state"] == "deduplicated"
assert conflict["error_code"] == "version_conflict"
assert stale["error_code"] == "invalid"
assert newer["ok"] and newer["version"] == 2
stored = run(service.store.get_memory(first["record_id"]))
assert stored.metadata["memory_domain"] == BOT_PERSONAL_MEMORY_DOMAIN
assert stored.metadata["subject"] == "bot_self"
assert stored.metadata["source_kind"] == TYPE_CONTRACTS["bot_observed_activity"][0]
assert stored.metadata["evidence_level"] == TYPE_CONTRACTS["bot_observed_activity"][1]
finally:
service.store.close()
def test_bridge_missing_or_exception_degrades_and_consumer_dead_letters():
missing = run(MemoryCompanionBridge(object()).record_bot_personal_archive(envelope()))
assert missing["state"] == "degraded"
assert missing["error_code"] == "bridge_method_unavailable"
class Broken:
async def record_bot_personal_archive(self, _envelope):
raise RuntimeError("unavailable")
consumer = BotPersonalConsumer(MemoryCompanionBridge(Broken()), max_attempts=2)
first = run(consumer.consume_bot_personal_archive(envelope(), attempt=1))
last = run(consumer.consume_bot_personal_archive(envelope(), attempt=2))
assert first["state"] == "retry"
assert last["state"] == "dead_letter"
def test_domain_isolation_and_read_only_profile_does_not_leak_payload(tmp_path):
service = make_service(tmp_path)
try:
bot = run(service.record_bot_personal_archive(envelope("bot")))
user = MemoryRecord(id="user-1", memory_type="user_fact", scope="private", session_id="private:user", visibility="private_pair", content="user record", metadata={"memory_domain": "user_memory"})
group = MemoryRecord(id="group-1", memory_type="group_memory", scope="group", session_id="group:g", visibility="group", content="group record", metadata={"memory_domain": "group_memory"})
run(service.store.insert_memory(user))
run(service.store.insert_memory(group))
profile = run(service.read_bot_personal_profile(limit=10))
assert profile["read_only"] is True
assert [item["record_id"] for item in profile["items"]] == [bot["record_id"]]
assert all("payload" not in item and "content" not in item for item in profile["items"])
assert "safe archive summary" not in str(profile)
finally:
service.store.close()
def test_distinct_bot_personal_idempotency_keys_remain_distinct_rows(tmp_path):
service = make_service(tmp_path)
try:
first = run(service.record_bot_personal_archive(envelope("one")))
second = run(service.record_bot_personal_archive(envelope("two")))
assert first["record_id"] != second["record_id"]
profile = run(service.read_bot_personal_profile(limit=10))
assert {item["record_id"] for item in profile["items"]} == {first["record_id"], second["record_id"]}
finally:
service.store.close()