astrbot_plugin_private_comp.../bot_personal_outbox.py
agent-bot 85d5069bbd feat: complete M6.1.3-R2 through M6.1.7 bot personal archive contract
M6.1.3-R2 非五类兼容(阻断修复):DTO date 缺失时从 occurred_at 派生,
刻意不做时区换算以与 _infer_window 同一时钟;calendar_event 取 start_at、
creative_progress 取 project.updated_at;未知 kind 不再默认 bot_observed_activity;
capture_hard_fact L4→L3;TYPE_CONTRACTS 覆盖全部 12 类,修复非五类 source_kind
兜底成 observed 污染本地记录;proactive_message 的 image_path 改为 has_image+
image_name(原样透传会被自身隐私校验整条静默丢弃);snapshot/daily_plan 透传 version。

M6.1.4 创作/媒体/主观分域:creative_progress 补 chunk 形参(生产调用点一直 TypeError
导致创作推进整步被阻断)并只留里程碑摘要;photo_generation 改为媒体索引
(原 payload 带 prompt 键与绝对路径,100% 静默丢弃);幂等键纳入隐私校验;
禁用键清单收窄,放行媒体标识键;两条 Bridge 路径统一送 body payload。

M6.1.5 Bot 专用检索 Profiles:按 Profile 分槽注入,固定槽位与条数、跨槽去重、
声明当前以本地运行态为准;旧版记忆插件回退通用检索。

M6.1.6 分层只读 API:新增 build_agenda_runtime_view 与 GET /agenda/runtime,
记忆插件停用时仍完整返回并标降级。

M6.1.7 隐私与残余收口:DTO 新增 contexts 禁用键;已删除娱乐功能(qzone/news/
web_exploration)的残余配置入口从展示面隐藏,旧值保留不删。

测试:174 项通过(pytest 与 unittest 双口径)。联合 E2E 135 项全部通过。

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 01:37:40 +08:00

148 行
6.3 KiB
Python

# -*- coding: utf-8 -*-
"""Small durable outbox for optional Bot personal archive delivery."""
from __future__ import annotations
from copy import deepcopy
from datetime import datetime, timedelta
import hashlib
import logging
from typing import Any
try:
from .bot_personal_dto import PrivacyRejected, validate_bot_personal_payload
except ImportError:
from bot_personal_dto import PrivacyRejected, validate_bot_personal_payload
OUTBOX_VERSION = 2
OUTBOX_STATES = {"pending", "sent", "retry", "dead_letter"}
logger = logging.getLogger("private_companion.bot_personal_outbox")
def _text(value: Any, limit: int = 240) -> str:
return str(value or "").strip()[:limit]
def _now(now: datetime | None = None) -> datetime:
return now or datetime.now().astimezone()
def _iso(now: datetime | None = None) -> str:
return _now(now).isoformat(timespec="seconds")
def _entry_id(memory_type: str, key: str) -> str:
digest = hashlib.sha256(f"{memory_type}|{key}".encode("utf-8")).hexdigest()[:20]
return f"outbox-{digest}"
def _same_key(item: dict[str, Any], memory_type: str, idempotency_key: str) -> bool:
return str(item.get("memory_type") or "") == memory_type and str(item.get("idempotency_key") or "") == idempotency_key
def ensure_outbox(data: dict[str, Any]) -> tuple[list[dict[str, Any]], bool]:
changed = False
if not isinstance(data.get("bot_personal_outbox"), list):
data["bot_personal_outbox"] = []
changed = True
if data.get("bot_personal_outbox_version") != OUTBOX_VERSION:
data["bot_personal_outbox_version"] = OUTBOX_VERSION
changed = True
return data["bot_personal_outbox"], changed
def _capacity_rejection(memory_type: str, key: str, version: int, payload: dict[str, Any]) -> dict[str, Any]:
return {
"accepted": False, "outbox_id": _entry_id(memory_type, key), "memory_type": memory_type,
"idempotency_key": key, "version": version, "payload": deepcopy(payload), "state": "rejected",
"attempts": 0, "created_at": "", "updated_at": "", "next_retry_at": "",
"last_error_code": "outbox_capacity",
}
def enqueue_outbox(
data: dict[str, Any], *, memory_type: str, idempotency_key: str, version: int, payload: dict[str, Any],
now: datetime | None = None, max_items: int = 500,
) -> dict[str, Any]:
validate_bot_personal_payload(payload)
records, _ = ensure_outbox(data)
memory_type = _text(memory_type, 80)
key = _text(idempotency_key, 240)
version = max(1, int(version or 1))
current = _iso(now)
existing = next((item for item in records if isinstance(item, dict) and _same_key(item, memory_type, key)), None)
if existing is not None:
old_version = max(1, int(existing.get("version") or 1))
if version < old_version:
result = deepcopy(existing)
result["accepted"] = False
result["last_error_code"] = "stale_version"
return result
if version == old_version:
if existing.get("payload") == payload:
return deepcopy(existing)
result = deepcopy(existing)
result["accepted"] = False
result["last_error_code"] = "version_conflict"
return result
existing.update({
"accepted": True, "version": version, "payload": deepcopy(payload), "state": "pending", "attempts": 0,
"updated_at": current, "next_retry_at": current, "last_error_code": "",
})
return deepcopy(existing)
if max_items > 0:
while len(records) >= max_items:
sent_index = next((index for index, item in enumerate(records) if isinstance(item, dict) and item.get("state") == "sent"), None)
if sent_index is None:
logger.warning("[PrivateCompanion] Bot personal outbox capacity reached; refusing new entry without dropping unfinished work: type=%s key=%s", memory_type, key)
return _capacity_rejection(memory_type, key, version, payload)
records.pop(sent_index)
record = {
"accepted": True, "outbox_id": _entry_id(memory_type, key), "memory_type": memory_type, "idempotency_key": key,
"version": version, "payload": deepcopy(payload), "state": "pending", "attempts": 0,
"created_at": current, "updated_at": current, "next_retry_at": current, "last_error_code": "",
}
records.append(record)
return deepcopy(record)
def update_outbox(
data: dict[str, Any], *, idempotency_key: str, state: str, memory_type: str = "", now: datetime | None = None,
error_code: str = "", max_attempts: int = 6, base_delay_seconds: int = 15,
) -> dict[str, Any] | None:
records, _ = ensure_outbox(data)
key = _text(idempotency_key, 240)
kind = _text(memory_type, 80)
record = next((item for item in records if isinstance(item, dict) and str(item.get("idempotency_key") or "") == key and (not kind or str(item.get("memory_type") or "") == kind)), None)
if record is None:
return None
current = _now(now)
attempts = int(record.get("attempts") or 0)
if state in {"retry", "dead_letter"}:
attempts += 1
if state == "retry" and attempts >= max_attempts:
state = "dead_letter"
record.update({"accepted": True, "state": state if state in OUTBOX_STATES else "retry", "attempts": attempts, "updated_at": _iso(current), "last_error_code": _text(error_code, 120)})
if record["state"] == "retry":
delay = min(3600, base_delay_seconds * (2 ** max(0, attempts - 1)))
record["next_retry_at"] = _iso(current + timedelta(seconds=delay))
elif record["state"] == "sent":
record["next_retry_at"] = ""
return deepcopy(record)
def due_outbox(data: dict[str, Any], *, now: datetime | None = None, limit: int = 20) -> list[dict[str, Any]]:
records, _ = ensure_outbox(data)
current = _now(now)
result = []
for record in records:
if not isinstance(record, dict) or record.get("state") not in {"pending", "retry"}:
continue
try:
due = datetime.fromisoformat(str(record.get("next_retry_at") or "").replace("Z", "+00:00"))
if due.tzinfo is None:
due = due.replace(tzinfo=current.tzinfo)
except ValueError:
due = current
if due <= current:
result.append(deepcopy(record))
return result[: max(1, min(100, int(limit or 20)))]