149 行
6.3 KiB
Python
149 行
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)))]
|