diff --git a/activity_capture.py b/activity_capture.py new file mode 100644 index 0000000..a2a371f --- /dev/null +++ b/activity_capture.py @@ -0,0 +1,151 @@ +# -*- coding: utf-8 -*- +"""Low-noise, local activity capture for chat-side C3 agenda data.""" +from __future__ import annotations + +from datetime import datetime, timedelta +from typing import Any + +try: + from .agenda_contracts import normalize_observed_activity, stable_id +except ImportError: + from agenda_contracts import normalize_observed_activity, stable_id + + +COLLABORATION_HINTS = { + "代码", "插件", "开发", "排障", "测试", "部署", "运维", "项目", "创作", "一起", "继续", + "code", "plugin", "debug", "test", "deploy", "project", "writing", +} + + +def classify_interaction(text: str, *, message_count: int = 1) -> str: + content = str(text or "").strip() + lowered = content.lower() + if not content: + return "ordinary" + if any(token in content or token in lowered for token in COLLABORATION_HINTS) and len(content) >= 4: + return "sustained" if message_count >= 2 else "candidate" + if message_count >= 4 and len(content) >= 12: + return "sustained" + return "short" if len(content) > 12 else "ordinary" + + +class ActivityCapture: + """Aggregate messages by conversation and time bucket before emitting facts.""" + + def __init__(self, *, window_minutes: int = 30, min_sustained_messages: int = 3): + self.window_minutes = max(5, int(window_minutes)) + self.min_sustained_messages = max(2, int(min_sustained_messages)) + self._buckets: dict[tuple[str, str], dict[str, Any]] = {} + + def _bucket_start(self, event_time: datetime) -> datetime: + current = event_time.replace(second=0, microsecond=0) + return current - timedelta(minutes=current.minute % self.window_minutes) + + def capture_message( + self, + *, + text: str, + event_time: datetime, + source_ref: str, + conversation_id: str, + participant: str = "user", + message_count: int = 1, + topic: str = "", + visibility: str = "private", + ) -> dict[str, Any] | None: + conversation = str(conversation_id or "").strip() or "unknown" + bucket_start = self._bucket_start(event_time) + key = (conversation, bucket_start.isoformat()) + bucket = self._buckets.setdefault( + key, + { + "conversation_id": conversation, + "bucket_start": bucket_start, + "bucket_end": bucket_start + timedelta(minutes=self.window_minutes), + "texts": [], + "source_refs": [], + "participants": [], + "count": 0, + "first_at": event_time, + "last_at": event_time, + "topic": str(topic or "").strip(), + "visibility": str(visibility or "private"), + }, + ) + ref = str(source_ref or "").strip() + is_duplicate = bool(ref and ref in bucket["source_refs"]) + if not is_duplicate: + bucket["count"] = max(int(bucket["count"]) + 1, int(message_count or 0), 1) + if ref: + bucket["source_refs"].append(ref) + clean_text = " ".join(str(text or "").split())[:240] + if clean_text: + bucket["texts"].append(clean_text) + person = str(participant or "user").strip() + if person and person not in bucket["participants"]: + bucket["participants"].append(person) + bucket["first_at"] = min(bucket["first_at"], event_time) + bucket["last_at"] = max(bucket["last_at"], event_time) + if topic and not bucket["topic"]: + bucket["topic"] = str(topic).strip() + + interaction = classify_interaction(text, message_count=int(bucket["count"])) + if int(bucket["count"]) < self.min_sustained_messages: + return None + + title = bucket["topic"] or (bucket["texts"][-1] if bucket["texts"] else "持续互动") + activity_id = stable_id("activity", conversation, bucket_start.isoformat(), bucket["topic"] or "conversation") + return normalize_observed_activity( + { + "activity_id": activity_id, + "title": title, + "summary": ";".join(bucket["texts"][-3:])[:360], + "kind": "conversation_activity", + "start_at": bucket["first_at"].isoformat(), + "end_at": bucket["last_at"].isoformat(), + "source": "conversation", + "source_refs": list(bucket["source_refs"]), + "participants": list(bucket["participants"]) + (["bot"] if "bot" not in bucket["participants"] else []), + "evidence_level": "L2", + "visibility": bucket["visibility"], + "certainty": "high" if bucket["count"] >= self.min_sustained_messages + 1 else "medium", + "status": "active", + }, + now=event_time, + ) + + def capture_hard_fact( + self, + *, + title: str, + start_at: datetime, + end_at: datetime | None, + source: str, + source_refs: list[str] | tuple[str, ...], + participants: list[str] | None = None, + kind: str = "tool_activity", + visibility: str = "private", + certainty: str = "high", + ) -> dict[str, Any]: + return normalize_observed_activity( + { + "activity_id": stable_id("activity", source, title, start_at.isoformat(), list(source_refs)), + "title": title, + "kind": kind, + "start_at": start_at.isoformat(), + "end_at": end_at.isoformat() if end_at else "", + "source": source, + "source_refs": list(source_refs), + "participants": participants or ["bot"], + "evidence_level": "L3", + "visibility": visibility, + "certainty": certainty, + "status": "completed" if end_at else "active", + }, + now=start_at, + ) + + def capture_tool_activity(self, **kwargs: Any) -> dict[str, Any]: + """Small named alias for callers that capture a tool-side hard fact.""" + + return self.capture_hard_fact(**kwargs) diff --git a/agenda_contracts.py b/agenda_contracts.py new file mode 100644 index 0000000..e0b485a --- /dev/null +++ b/agenda_contracts.py @@ -0,0 +1,496 @@ +# -*- coding: utf-8 -*- +"""Local C3 agenda contracts for the chat-side companion plugin. + +The schedule definition is deliberately imported from ``bot_personal_contract`` +so this module cannot drift into a second set of window thresholds. Everything +stored by this module is a plain JSON-compatible dictionary or scalar. +""" +from __future__ import annotations + +from copy import deepcopy +from datetime import date, datetime, time, timedelta +import hashlib +import json +from typing import Any, Iterable +from zoneinfo import ZoneInfo + +try: # package import + from .bot_personal_contract import ( + SCHEDULE_WINDOWS as _CONTRACT_WINDOWS, + WINDOW_SLUGS as _CONTRACT_WINDOW_SLUGS, + window_for_minutes as _contract_window_for_minutes, + ) +except ImportError: # direct test/import from the plugin directory + from bot_personal_contract import ( + SCHEDULE_WINDOWS as _CONTRACT_WINDOWS, + WINDOW_SLUGS as _CONTRACT_WINDOW_SLUGS, + window_for_minutes as _contract_window_for_minutes, + ) + + +AGENDA_VERSION = 1 +SCHEDULE_WINDOWS = tuple(_CONTRACT_WINDOWS) +WINDOW_SLUGS = tuple(_CONTRACT_WINDOW_SLUGS) +SOURCE_KINDS = {"planned", "observed", "projection", "reconciled"} +EVIDENCE_LEVELS = {"L0", "L1", "L2", "L3", "L4", "L5"} +AGENDA_STATUSES = { + "planned", + "active", + "completed", + "partially_completed", + "overridden", + "reconciled", + "deferred", + "cancelled", + "unknown", +} + + +class AgendaContractError(ValueError): + """Raised when a value cannot satisfy the local agenda contract.""" + + +def _text(value: Any, limit: int = 240) -> str: + if value is None: + return "" + return str(value).strip()[:limit] + + +def _list(value: Any, limit: int = 30) -> list[str]: + if isinstance(value, str): + value = [value] + if not isinstance(value, (list, tuple, set)): + return [] + result: list[str] = [] + for item in value: + text = _text(item, 200) + if text and text not in result: + result.append(text) + if len(result) >= limit: + break + return result + +def _items(value: Any, limit: int = 40) -> list[Any]: + if not isinstance(value, list): + return [] + return deepcopy(value[:limit]) + + +def _version(value: Any, default: int = 1) -> int: + try: + return max(1, int(value or default)) + except (TypeError, ValueError): + return default + + +def _now_iso(now: datetime | None = None) -> str: + current = now or datetime.now().astimezone() + if current.tzinfo is None: + current = current.replace(tzinfo=ZoneInfo("Asia/Shanghai")) + return current.isoformat(timespec="seconds") + + +def stable_id(prefix: str, *parts: Any) -> str: + """Return a deterministic, order-stable identifier for JSON-like parts.""" + + def canonical(value: Any) -> Any: + if isinstance(value, dict): + return {str(key): canonical(value[key]) for key in sorted(value, key=str)} + if isinstance(value, (list, tuple)): + return [canonical(item) for item in value] + if isinstance(value, set): + return sorted(canonical(item) for item in value) + if isinstance(value, (datetime, date, time)): + return value.isoformat() + return value + + raw = json.dumps(canonical(parts), ensure_ascii=False, sort_keys=True, default=str, separators=(",", ":")) + digest = hashlib.sha256(raw.encode("utf-8")).hexdigest()[:20] + return f"{_text(prefix, 48) or 'agenda'}-{digest}" + + +def timezone_or_default(timezone_name: Any = "Asia/Shanghai") -> ZoneInfo: + try: + return ZoneInfo(_text(timezone_name, 64) or "Asia/Shanghai") + except Exception: + return ZoneInfo("Asia/Shanghai") + + +def normalize_window(value: Any) -> str: + candidate = _text(value, 48).lower() + return candidate if candidate in WINDOW_SLUGS else "" + + +def window_for_minutes(minutes: Any) -> str: + """Delegate minute classification to the shared chat-side contract.""" + + try: + return _contract_window_for_minutes(int(minutes)) + except (TypeError, ValueError): + return "" + + +def _window_spec(window: Any) -> tuple[str, str, int, int]: + slug = normalize_window(window) + for item in SCHEDULE_WINDOWS: + if item[0] == slug: + return item + raise AgendaContractError(f"unknown window: {window!r}") + + +def _as_date(value: Any) -> date: + if isinstance(value, datetime): + return value.date() + if isinstance(value, date): + return value + try: + return date.fromisoformat(_text(value, 32)) + except ValueError as exc: + raise AgendaContractError(f"invalid date: {value!r}") from exc + + +def parse_datetime(value: Any, *, timezone_name: str = "Asia/Shanghai", default: datetime | None = None) -> datetime: + """Parse ISO/date/time values and attach the requested local timezone.""" + + tz = timezone_or_default(timezone_name) + if isinstance(value, datetime): + current = value + elif isinstance(value, date): + current = datetime.combine(value, time.min) + else: + text = _text(value, 96) + if not text: + if default is None: + raise AgendaContractError("datetime is required") + current = default + else: + if text.endswith("Z"): + text = text[:-1] + "+00:00" + try: + current = datetime.fromisoformat(text) + except ValueError: + try: + current = datetime.strptime(text, "%H:%M") + except ValueError as exc: + raise AgendaContractError(f"invalid datetime: {value!r}") from exc + if current.tzinfo is None: + return current.replace(tzinfo=tz) + return current.astimezone(tz) + + +def window_bounds( + window_date: str | date, + window: str, + *, + timezone_name: str = "Asia/Shanghai", +) -> tuple[datetime, datetime]: + """Return inclusive-start/exclusive-end aware bounds for a window date.""" + + _slug, _name, start_minute, end_minute = _window_spec(window) + target = _as_date(window_date) + tz = timezone_or_default(timezone_name) + start = datetime.combine(target, time.min, tzinfo=tz) + timedelta(minutes=start_minute) + end_date = target + timedelta(days=1) if end_minute <= start_minute else target + end = datetime.combine(end_date, time.min, tzinfo=tz) + timedelta(minutes=end_minute) + return start, end + + +def window_for_datetime( + value: datetime | date | str, + timezone_name: str = "Asia/Shanghai", +) -> tuple[str, str, datetime, datetime]: + """Resolve a moment to ``(slug, window_date, start, end)``. + + The early-morning part of ``late_night`` belongs to the preceding + ``window_date``. Minute classification always goes through the shared + ``bot_personal_contract.window_for_minutes`` implementation. + """ + + current = parse_datetime(value, timezone_name=timezone_name) + minute = current.hour * 60 + current.minute + slug = window_for_minutes(minute) + if not slug: + raise AgendaContractError(f"no window for minute: {minute}") + _slug, _name, start_minute, end_minute = _window_spec(slug) + belongs_to_previous_date = end_minute <= start_minute and minute < end_minute + target_date = current.date() - timedelta(days=1) if belongs_to_previous_date else current.date() + start, end = window_bounds(target_date, slug, timezone_name=timezone_name) + return slug, target_date.isoformat(), start, end + + +def window_for_plan_minutes( + plan_date: str | date, + minutes: Any, + *, + timezone_name: str = "Asia/Shanghai", +) -> tuple[str, str]: + """Resolve a plan date plus possibly out-of-range minute offset.""" + + try: + base = _as_date(plan_date) + value = int(minutes) + except (TypeError, ValueError, AgendaContractError): + return "", "" + day_offset, raw_minute = divmod(value, 24 * 60) + moment = datetime.combine(base + timedelta(days=day_offset), time.min) + moment += timedelta(minutes=raw_minute) + slug, target_date, _start, _end = window_for_datetime(moment, timezone_name=timezone_name) + return slug, target_date + + +def interval_overlaps_window( + item: dict[str, Any], + start: datetime, + end: datetime, + *, + timezone_name: str = "Asia/Shanghai", +) -> bool: + """Return whether an item interval intersects ``[start, end)``.""" + + if not isinstance(item, dict): + return False + try: + item_start = parse_datetime(item.get("start_at") or item.get("start"), timezone_name=timezone_name) + except AgendaContractError: + return False + try: + item_end = parse_datetime( + item.get("end_at") or item.get("end"), + timezone_name=timezone_name, + default=item_start, + ) + except AgendaContractError: + item_end = item_start + if item_end <= item_start: + item_end = item_start + timedelta(seconds=1) + start_local = parse_datetime(start, timezone_name=timezone_name) + end_local = parse_datetime(end, timezone_name=timezone_name) + return item_start < end_local and item_end > start_local + + +def normalize_source_kind(value: Any, default: str) -> str: + candidate = _text(value, 32).lower() + return candidate if candidate in SOURCE_KINDS else default + + +def normalize_evidence_level(value: Any, default: str) -> str: + candidate = _text(value, 8).upper() + return candidate if candidate in EVIDENCE_LEVELS else default + + +def _normalize_status(value: Any, default: str) -> str: + candidate = _text(value, 32).lower() + return candidate if candidate in AGENDA_STATUSES else default + + +def normalize_plan_item( + raw: dict[str, Any], + *, + plan_id: str = "", + now: datetime | None = None, +) -> dict[str, Any]: + if not isinstance(raw, dict): + raise AgendaContractError("plan item must be an object") + title = _text(raw.get("title") or raw.get("activity") or raw.get("description"), 240) + if not title: + raise AgendaContractError("plan item requires a title") + result = deepcopy(raw) + result.update( + { + "plan_id": _text(plan_id or raw.get("plan_id") or raw.get("event_id"), 120) + or stable_id("plan", title, raw.get("date"), raw.get("time"), raw.get("start_at")), + "title": title, + "source_kind": "planned", + "evidence_level": normalize_evidence_level(raw.get("evidence_level"), "L0"), + "status": _normalize_status(raw.get("status"), "planned"), + "version": _version(raw.get("version")), + "source_refs": _list(raw.get("source_refs") or raw.get("basis")), + "visibility": _text(raw.get("visibility"), 32) or "private", + "certainty": _text(raw.get("certainty"), 24) or "medium", + "updated_at": _text(raw.get("updated_at"), 64) or _now_iso(now), + } + ) + return result + + +def normalize_observed_activity( + raw: dict[str, Any], + *, + now: datetime | None = None, +) -> dict[str, Any]: + if not isinstance(raw, dict): + raise AgendaContractError("observed activity must be an object") + title = _text(raw.get("title") or raw.get("summary") or raw.get("activity"), 240) + if not title: + raise AgendaContractError("observed activity requires a title") + source_refs = _list(raw.get("source_refs") or raw.get("evidence_refs")) + result = deepcopy(raw) + result.update( + { + "activity_id": _text(raw.get("activity_id") or raw.get("id"), 120) + or stable_id("activity", title, raw.get("start_at") or raw.get("start"), raw.get("end_at") or raw.get("end"), source_refs), + "title": title, + "kind": _text(raw.get("kind"), 48) or "conversation", + "source_kind": "observed", + "source": _text(raw.get("source"), 64) or "conversation", + "source_refs": source_refs, + "participants": _list(raw.get("participants")), + "evidence_level": normalize_evidence_level(raw.get("evidence_level"), "L2"), + "visibility": _text(raw.get("visibility"), 32) or "private", + "certainty": _text(raw.get("certainty"), 24) or "medium", + "status": _normalize_status(raw.get("status"), "active"), + "version": _version(raw.get("version")), + "updated_at": _text(raw.get("updated_at"), 64) or _now_iso(now), + } + ) + return result + + +def normalize_window_snapshot( + raw: dict[str, Any], + *, + now: datetime | None = None, +) -> dict[str, Any]: + if not isinstance(raw, dict): + raise AgendaContractError("window snapshot must be an object") + window_date = _text(raw.get("window_date") or raw.get("date"), 20) + window = normalize_window(raw.get("window") or raw.get("slug")) + if not window_date or not window: + raise AgendaContractError("window snapshot requires window_date and window") + result = deepcopy(raw) + result.update( + { + "snapshot_id": _text(raw.get("snapshot_id"), 160) or stable_id("agenda_snapshot", window_date, window), + "date": _text(raw.get("date"), 20) or window_date, + "window_date": window_date, + "window": window, + "planned": _items(raw.get("planned")), + "observed": _items(raw.get("observed")), + "reconciled": _items(raw.get("reconciled")), + "open_items": _list(raw.get("open_items")), + "source_refs": _list(raw.get("source_refs")), + "source_kind": "projection", + "evidence_level": normalize_evidence_level(raw.get("evidence_level"), "L2"), + "visibility": _text(raw.get("visibility"), 32) or "private", + "certainty": _text(raw.get("certainty"), 24) or "medium", + "status": _normalize_status(raw.get("status"), "completed"), + "version": _version(raw.get("version")), + "generated_at": _text(raw.get("generated_at"), 64) or _now_iso(now), + "timezone": _text(raw.get("timezone"), 64) or "Asia/Shanghai", + } + ) + return result + + +def normalize_reconciliation(raw: dict[str, Any], *, now: datetime | None = None) -> dict[str, Any]: + if not isinstance(raw, dict): + raise AgendaContractError("reconciliation must be an object") + window_date = _text(raw.get("window_date") or raw.get("date"), 20) + window = normalize_window(raw.get("window") or raw.get("slug")) + result = deepcopy(raw) + result.update( + { + "reconciliation_id": _text(raw.get("reconciliation_id"), 160) + or stable_id("reconciliation", window_date, window), + "window_date": window_date, + "date": _text(raw.get("date"), 20) or window_date, + "window": window, + "source_kind": "reconciled", + "evidence_level": normalize_evidence_level(raw.get("evidence_level"), "L3"), + "visibility": _text(raw.get("visibility"), 32) or "private", + "certainty": _text(raw.get("certainty"), 24) or "high", + "status": _normalize_status(raw.get("status"), "completed"), + "source_refs": _list(raw.get("source_refs")), + "version": _version(raw.get("version")), + "generated_at": _text(raw.get("generated_at"), 64) or _now_iso(now), + } + ) + return result + + +def migrate_store(data: dict[str, Any]) -> tuple[dict[str, Any], bool]: + """Additive migration for old plugin JSON without deleting old fields.""" + + if not isinstance(data, dict): + raise AgendaContractError("store must be an object") + changed = False + defaults: dict[str, Any] = { + "agenda_version": AGENDA_VERSION, + "observed_activities": [], + "window_snapshots": [], + "agenda_reconciliation_history": [], + } + for key, default in defaults.items(): + if key not in data: + data[key] = deepcopy(default) + changed = True + elif not isinstance(data.get(key), type(default)): + data[f"legacy_{key}"] = deepcopy(data[key]) + data[key] = deepcopy(default) + changed = True + + if "activities" in data and not data["observed_activities"] and isinstance(data["activities"], list): + data["observed_activities"] = deepcopy(data["activities"]) + changed = True + + try: + current_version = max(0, int(data.get("agenda_version") or 0)) + except (TypeError, ValueError): + current_version = 0 + if current_version < AGENDA_VERSION: + data["agenda_version"] = AGENDA_VERSION + changed = True + + if isinstance(data.get("daily_plan"), dict) and isinstance(data["daily_plan"].get("items"), list): + migrated_items: list[Any] = [] + for item in data["daily_plan"]["items"]: + if not isinstance(item, dict): + migrated_items.append(item) + continue + try: + migrated_items.append(normalize_plan_item(item)) + except AgendaContractError: + migrated_items.append(deepcopy(item)) + if migrated_items != data["daily_plan"]["items"]: + data["daily_plan"]["items"] = migrated_items + changed = True + return data, changed + + +def agenda_entry_from_plan(plan: dict[str, Any], *, reason: str = "") -> dict[str, Any]: + result = { + "entry_id": stable_id("agenda_entry", "plan", plan.get("plan_id")), + "title": _text(plan.get("title") or plan.get("activity"), 240), + "kind": "planned", + "status": _normalize_status(plan.get("status"), "planned"), + "source_kind": "planned", + "evidence_level": normalize_evidence_level(plan.get("evidence_level"), "L0"), + "visibility": _text(plan.get("visibility"), 32) or "private", + "certainty": _text(plan.get("certainty"), 24) or "medium", + "source_refs": _list(plan.get("source_refs")) or [_text(plan.get("plan_id"), 120)], + } + if reason: + result["reconciliation_reason"] = reason + for key in ("plan_id", "start_at", "end_at", "date", "time", "end", "participants", "version"): + if key in plan: + result[key] = deepcopy(plan[key]) + return result + + +def agenda_entry_from_activity(activity: dict[str, Any], *, source_refs: Iterable[str] = ()) -> dict[str, Any]: + refs = _list(list(source_refs)) or _list(activity.get("source_refs")) or [_text(activity.get("activity_id"), 120)] + result = { + "entry_id": stable_id("agenda_entry", "activity", activity.get("activity_id")), + "title": _text(activity.get("title"), 240), + "kind": "observed", + "status": _normalize_status(activity.get("status"), "active"), + "source_kind": "observed", + "evidence_level": normalize_evidence_level(activity.get("evidence_level"), "L2"), + "visibility": _text(activity.get("visibility"), 32) or "private", + "certainty": _text(activity.get("certainty"), 24) or "medium", + "source_refs": refs, + } + for key in ("activity_id", "start_at", "end_at", "participants", "version", "kind"): + if key in activity: + result[key] = deepcopy(activity[key]) + return result diff --git a/agenda_runtime.py b/agenda_runtime.py new file mode 100644 index 0000000..a2d1ab2 --- /dev/null +++ b/agenda_runtime.py @@ -0,0 +1,278 @@ +# -*- coding: utf-8 -*- +"""Pure local-data runtime mixin for the chat-side C3 agenda.""" +from __future__ import annotations + +from copy import deepcopy +from datetime import date, datetime, timedelta +from typing import Any + +try: + from .activity_capture import ActivityCapture + from .agenda_contracts import ( + SCHEDULE_WINDOWS, + interval_overlaps_window, + migrate_store, + normalize_observed_activity, + normalize_plan_item, + normalize_reconciliation, + normalize_window_snapshot, + stable_id, + window_bounds, + ) + from .schedule_reconciler import reconcile + from .unified_agenda import build_unified_agenda, format_agenda_context +except ImportError: + from activity_capture import ActivityCapture + from agenda_contracts import ( + SCHEDULE_WINDOWS, + interval_overlaps_window, + migrate_store, + normalize_observed_activity, + normalize_plan_item, + normalize_reconciliation, + normalize_window_snapshot, + stable_id, + window_bounds, + ) + from schedule_reconciler import reconcile + from unified_agenda import build_unified_agenda, format_agenda_context + + +class AgendaRuntimeMixin: + """Keep C3 state in ``self.data`` and nowhere else.""" + + def _agenda_timezone_name(self) -> str: + getter = getattr(self, "_calendar_timezone_name", None) + if callable(getter): + try: + return str(getter() or "Asia/Shanghai") + except Exception: + pass + return str(getattr(self, "calendar_timezone", "Asia/Shanghai") or "Asia/Shanghai") + + def _agenda_now(self) -> datetime: + getter = getattr(self, "_calendar_now", None) + if callable(getter): + try: + return getter() + except Exception: + pass + return datetime.now().astimezone() + + def _agenda_prepare_store(self) -> None: + if not isinstance(getattr(self, "data", None), dict): + self.data = {} + migrated, changed = migrate_store(self.data) + self.data = migrated + if changed: + self._agenda_migration_dirty = True + if not isinstance(getattr(self, "_agenda_capture", None), ActivityCapture): + self._agenda_capture = ActivityCapture() + + def _agenda_activities_store(self) -> list[dict[str, Any]]: + self._agenda_prepare_store() + return self.data["observed_activities"] + + def _agenda_snapshots_store(self) -> list[dict[str, Any]]: + self._agenda_prepare_store() + return self.data["window_snapshots"] + + def _agenda_reconciliation_store(self) -> list[dict[str, Any]]: + self._agenda_prepare_store() + return self.data["agenda_reconciliation_history"] + + def _agenda_capture_inbound_message( + self, + *, + text: str, + event_time: datetime, + source_ref: str, + conversation_id: str, + participant: str = "user", + message_count: int = 1, + topic: str = "", + visibility: str = "private", + ) -> dict[str, Any] | None: + self._agenda_prepare_store() + candidate = self._agenda_capture.capture_message( + text=text, + event_time=event_time, + source_ref=source_ref, + conversation_id=conversation_id, + participant=participant, + message_count=message_count, + topic=topic, + visibility=visibility, + ) + if candidate is None: + return None + activities = self._agenda_activities_store() + activity_id = candidate.get("activity_id") + existing = next((item for item in activities if item.get("activity_id") == activity_id), None) + if existing is None: + activities.append(deepcopy(candidate)) + result = candidate + else: + existing_refs = list(existing.get("source_refs") or []) + for ref in candidate.get("source_refs") or []: + if ref not in existing_refs: + existing_refs.append(ref) + existing.update(deepcopy(candidate)) + existing["source_refs"] = existing_refs[:50] + existing["version"] = int(existing.get("version") or 1) + 1 + result = deepcopy(existing) + activities[:] = activities[-500:] + return result + + def _agenda_capture_hard_fact(self, activity: dict[str, Any]) -> dict[str, Any]: + self._agenda_prepare_store() + normalized = normalize_observed_activity(activity, now=self._agenda_now()) + activities = self._agenda_activities_store() + existing = next((item for item in activities if item.get("activity_id") == normalized.get("activity_id")), None) + if existing is None: + activities.append(normalized) + result = deepcopy(normalized) + else: + existing.update(deepcopy(normalized)) + existing["version"] = int(existing.get("version") or 1) + 1 + result = deepcopy(existing) + activities[:] = activities[-500:] + return result + + def _agenda_current_plan_items(self) -> list[dict[str, Any]]: + self._agenda_prepare_store() + plan = self.data.get("daily_plan") if isinstance(self.data.get("daily_plan"), dict) else {} + plan_date = str(plan.get("date") or self._agenda_now().date().isoformat())[:10] + items = plan.get("items") if isinstance(plan.get("items"), list) else [] + result: list[dict[str, Any]] = [] + for index, raw in enumerate(items): + if not isinstance(raw, dict): + continue + item = deepcopy(raw) + item.setdefault("date", plan_date) + try: + normalized = normalize_plan_item(item, plan_id=str(item.get("plan_id") or f"{plan_date}:{index}"), now=self._agenda_now()) + except Exception: + continue + if normalized.get("start_at") is None: + clock = str(normalized.get("time") or normalized.get("start") or "").strip() + if clock: + normalized["start_at"] = f"{plan_date}T{clock}:00" if len(clock) == 5 else f"{plan_date}T{clock}" + if normalized.get("end_at") is None: + end_clock = str(normalized.get("end") or normalized.get("end_time") or "").strip() + if end_clock: + normalized["end_at"] = f"{plan_date}T{end_clock}:00" if len(end_clock) == 5 else f"{plan_date}T{end_clock}" + result.append(normalized) + return result + + def _agenda_build(self, *, date_key: str = "") -> dict[str, Any]: + self._agenda_prepare_store() + return build_unified_agenda( + plans=self._agenda_current_plan_items(), + activities=self._agenda_activities_store(), + now=self._agenda_now(), + date_key=date_key, + timezone_name=self._agenda_timezone_name(), + ) + + def _agenda_context_for_prompt(self, *, max_entries: int = 8) -> str: + return format_agenda_context(self._agenda_build(), max_entries=max_entries) + + def _agenda_snapshot_window( + self, + *, + date_key: str, + window: str, + open_items: list[str] | None = None, + ) -> dict[str, Any]: + self._agenda_prepare_store() + timezone_name = self._agenda_timezone_name() + start, end = window_bounds(date_key, window, timezone_name=timezone_name) + plans = [item for item in self._agenda_current_plan_items() if interval_overlaps_window(item, start, end, timezone_name=timezone_name)] + activities = [item for item in self._agenda_activities_store() if interval_overlaps_window(item, start, end, timezone_name=timezone_name)] + now = self._agenda_now() + settled = reconcile(plans, activities, now=now) + snapshot_id = f"agenda_snapshot:{date_key}:{window}" + snapshot = normalize_window_snapshot( + { + "snapshot_id": snapshot_id, + "date": date_key, + "window_date": date_key, + "window": window, + "start_at": start.isoformat(timespec="seconds"), + "end_at": end.isoformat(timespec="seconds"), + "timezone": timezone_name, + "planned": settled["plans"], + "observed": settled["activities"], + "reconciled": settled["reconciliations"], + "open_items": list(open_items or []), + "source_refs": [str(item.get("activity_id")) for item in settled["activities"] if item.get("activity_id")], + "certainty": "high" if settled["reconciliations"] else "medium", + }, + now=now, + ) + snapshots = self._agenda_snapshots_store() + existing = next((item for item in snapshots if item.get("snapshot_id") == snapshot_id), None) + if existing is None: + snapshots.append(snapshot) + else: + comparable_old = {key: value for key, value in existing.items() if key not in {"generated_at", "version"}} + comparable_new = {key: value for key, value in snapshot.items() if key not in {"generated_at", "version"}} + if comparable_old != comparable_new: + snapshot["version"] = int(existing.get("version") or 1) + 1 + existing.clear() + existing.update(snapshot) + else: + snapshot = deepcopy(existing) + snapshots[:] = snapshots[-240:] + + reconciliation = normalize_reconciliation( + { + "reconciliation_id": f"reconciliation:{date_key}:{window}", + "date": date_key, + "window_date": date_key, + "window": window, + "start_at": snapshot.get("start_at"), + "end_at": snapshot.get("end_at"), + "timezone": timezone_name, + "plans": settled["reconciliations"], + "observed_activity_ids": list(snapshot.get("source_refs") or []), + "source_refs": [snapshot_id], + "status": "completed", + }, + now=now, + ) + history = self._agenda_reconciliation_store() + old_record = next((item for item in history if item.get("reconciliation_id") == reconciliation["reconciliation_id"]), None) + if old_record is None: + history.append(reconciliation) + else: + old_record.update(reconciliation) + reconciliation = deepcopy(old_record) + history[:] = history[-480:] + return snapshot + + def _agenda_closed_windows(self, now: datetime) -> list[tuple[str, str]]: + self._agenda_prepare_store() + timezone_name = self._agenda_timezone_name() + local_now = now + if local_now.tzinfo is None: + local_now = local_now.replace(tzinfo=window_bounds(local_now.date(), "morning", timezone_name=timezone_name)[0].tzinfo) + existing = {str(item.get("snapshot_id")) for item in self._agenda_snapshots_store() if isinstance(item, dict)} + candidates: list[tuple[str, str]] = [] + for offset in range(-3, 2): + target = (local_now + timedelta(days=offset)).date() + for slug, _name, _start, _end in SCHEDULE_WINDOWS: + _window_start, window_end = window_bounds(target, slug, timezone_name=timezone_name) + snapshot_id = f"agenda_snapshot:{target.isoformat()}:{slug}" + if window_end <= local_now and snapshot_id not in existing: + candidates.append((target.isoformat(), slug)) + return candidates + + def _agenda_maintenance_tick(self) -> list[dict[str, Any]]: + self._agenda_prepare_store() + settled: list[dict[str, Any]] = [] + now = self._agenda_now() + for date_key, window in self._agenda_closed_windows(now): + settled.append(self._agenda_snapshot_window(date_key=date_key, window=window)) + return settled diff --git a/bot_personal_dto.py b/bot_personal_dto.py new file mode 100644 index 0000000..9092963 --- /dev/null +++ b/bot_personal_dto.py @@ -0,0 +1,259 @@ +# -*- coding: utf-8 -*- +"""Privacy-limited DTOs for the Bot Personal archive boundary.""" +from __future__ import annotations + +from copy import deepcopy +from dataclasses import dataclass +from datetime import datetime +import json +import re +from typing import Any + +from .bot_personal_contract import ( + BOT_PERSONAL_MEMORY_DOMAIN, + BOT_PERSONAL_MEMORY_TYPES, + BOT_PERSONAL_PAYLOAD_SCHEMA_VERSION, + BOT_PERSONAL_SUBJECT, + TYPE_CONTRACTS, + normalize_window, + window_for_minutes, +) + + +FORBIDDEN_KEY_PARTS = { + "prompt", "conversation", "chat_history", "transcript", "message_chain", "contexts", + "cookie", "token", "password", "passwd", "secret", "credential", "authorization", + "api_key", "apikey", "access_key", "private_key", "raw_message", "binary", + "media_bytes", "media_binary", "media_data", "media_content", "media_blob", + "image_bytes", "audio_bytes", "video_bytes", "base64", +} +CERTAINTY_ALIASES = {"high": 0.9, "medium": 0.6, "low": 0.3, "高": 0.9, "中": 0.6, "低": 0.3} + + +class PrivacyRejected(ValueError): + def __init__(self, path: str, reason: str) -> None: + self.path = path + self.reason = reason + super().__init__(f"privacy_rejected:{path}:{reason}") + + +def _text(value: Any, limit: int = 240) -> str: + return str(value or "").strip()[:limit] + + +def _certainty(value: Any, default: float = 0.6) -> float: + if isinstance(value, str): + value = CERTAINTY_ALIASES.get(value.strip().lower(), value) + try: + number = float(value) + except (TypeError, ValueError): + number = default + return max(0.0, min(1.0, number)) + + +def _parse_moment(value: Any) -> datetime | None: + try: + parsed = datetime.fromisoformat(str(value).replace("Z", "+00:00")) + except (TypeError, ValueError): + return None + return parsed + + +def _looks_sensitive_value(value: str) -> str: + text = value.strip() + lowered = text.lower() + if re.search(r"(?:password|passwd|token|secret|api[_-]?key|authorization)\s*[:=]", lowered): + return "credential_pattern" + if re.search(r"^bearer\s+", lowered): + return "authorization_value" + if "base64," in lowered or lowered.startswith("data:"): + return "base64_or_data_uri" + if "-----begin " in lowered: + return "private_key_material" + if re.search(r"(?:^|[\s=:;,])(?:[a-z]:[\\/]|\\\\|/home/|/root/|/tmp/|/var/|/volume\d+/)", text, re.IGNORECASE): + return "absolute_path" + return "" + + +def validate_bot_personal_key(value: Any, *, field: str = "idempotency_key") -> None: + reason = _looks_sensitive_value(str(value or "")) + if reason: + raise PrivacyRejected(field, reason) + if not _text(value, 240): + raise PrivacyRejected(field, "missing") + + +def validate_bot_personal_payload(value: Any, *, path: str = "payload", depth: int = 0) -> None: + if depth > 8: + raise PrivacyRejected(path, "max_depth") + if isinstance(value, dict): + if len(value) > 64: + raise PrivacyRejected(path, "too_many_fields") + for key, item in value.items(): + name = _text(key, 80) + lowered = name.lower().replace("-", "_") + if not name or any(part in lowered for part in FORBIDDEN_KEY_PARTS): + raise PrivacyRejected(f"{path}.{name}", "forbidden_key") + validate_bot_personal_payload(item, path=f"{path}.{name}", depth=depth + 1) + return + if isinstance(value, (list, tuple)): + if len(value) > 64: + raise PrivacyRejected(path, "too_many_items") + for index, item in enumerate(value): + validate_bot_personal_payload(item, path=f"{path}[{index}]", depth=depth + 1) + return + if isinstance(value, (bytes, bytearray, memoryview)): + raise PrivacyRejected(path, "binary_media") + if isinstance(value, str): + reason = _looks_sensitive_value(value) + if reason: + raise PrivacyRejected(path, reason) + return + if value is None or isinstance(value, (bool, int, float)): + return + raise PrivacyRejected(path, "unsupported_value") + + +def _safe_value(value: Any, *, depth: int = 0, max_depth: int = 8) -> Any: + if depth > max_depth: + return None + if isinstance(value, dict): + return { + _text(key, 80): _safe_value(item, depth=depth + 1, max_depth=max_depth) + for key, item in list(value.items())[:64] + if _text(key, 80) + } + if isinstance(value, (list, tuple)): + return [_safe_value(item, depth=depth + 1, max_depth=max_depth) for item in list(value)[:64]] + if isinstance(value, str): + return _text(value, 1200) + if value is None or isinstance(value, (bool, int, float)): + return value + return _text(value, 240) + + +def _derive_date(value: Any, occurred_at: str, now: datetime) -> str: + explicit = _text(value, 32) + if explicit: + return explicit.split("T", 1)[0] + parsed = _parse_moment(occurred_at) or now.astimezone() + return parsed.date().isoformat() + + +def _derive_window(value: Any, occurred_at: str, now: datetime) -> str: + explicit = normalize_window(value) + parsed = _parse_moment(occurred_at) or now.astimezone() + if explicit: + return explicit + return window_for_minutes(parsed.hour * 60 + parsed.minute) + + +@dataclass(frozen=True) +class BotPersonalArchiveDTO: + record_id: str + memory_domain: str + memory_type: str + subject: str + date: str + window: str + window_date: str + occurred_at: str + created_at: str + updated_at: str + source_kind: str + source_refs: list[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, + "window_date": self.window_date, + "occurred_at": self.occurred_at, + "created_at": self.created_at, + "updated_at": self.updated_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), + } + + +def build_bot_personal_dto( + *, + memory_type: str, + kind: str = "", + namespace: str = "", + payload: dict[str, Any], + idempotency_key: str, + occurred_at: str, + now: datetime | None = None, + version: int = 1, +) -> BotPersonalArchiveDTO: + del kind, namespace + if memory_type not in BOT_PERSONAL_MEMORY_TYPES: + raise ValueError(f"invalid_memory_type:{memory_type}") + validate_bot_personal_payload(payload) + validate_bot_personal_key(idempotency_key) + current = now or datetime.now().astimezone() + safe = _safe_value(payload) or {} + occurred = _text(occurred_at, 80) or current.isoformat(timespec="seconds") + date_key = _derive_date(safe.get("date") or safe.get("window_date"), occurred, current) + window = _derive_window(safe.get("window"), occurred, current) + if not window: + raise ValueError("invalid_window") + source_refs: list[str] = [] + for item in safe.get("source_refs") or []: + ref = _text(item, 240) + if ref and ref not in source_refs: + source_refs.append(ref) + if not source_refs: + source_refs = [f"archive:{_text(idempotency_key, 240)}"] + contract = TYPE_CONTRACTS[memory_type] + source_kind, default_evidence, default_status = contract + evidence = "L0" if memory_type == "bot_schedule_plan" else (_text(safe.get("evidence_level"), 8).upper() or default_evidence) + if evidence not in {"L0", "L1", "L2", "L3"}: + evidence = default_evidence + created_at = _text(safe.get("created_at"), 80) or current.isoformat(timespec="seconds") + updated_at = _text(safe.get("updated_at"), 80) or created_at + record_id = _text(safe.get("record_id"), 160) or f"local:{_text(idempotency_key, 240)}" + return BotPersonalArchiveDTO( + record_id=record_id, + memory_domain=BOT_PERSONAL_MEMORY_DOMAIN, + memory_type=memory_type, + subject=BOT_PERSONAL_SUBJECT, + date=date_key, + window=window, + window_date=date_key, + occurred_at=occurred, + created_at=created_at, + updated_at=updated_at, + source_kind=source_kind, + source_refs=source_refs, + certainty=_certainty(safe.get("certainty"), 0.6), + evidence_level=evidence, + status=_text(safe.get("status"), 32) or default_status, + version=max(1, int(version or 1)), + idempotency_key=_text(idempotency_key, 240), + payload_schema_version=BOT_PERSONAL_PAYLOAD_SCHEMA_VERSION, + payload=deepcopy(safe), + ) + + +def envelope_size_bytes(envelope: dict[str, Any]) -> int: + return len(json.dumps(envelope, ensure_ascii=False, separators=(",", ":")).encode("utf-8")) diff --git a/bot_personal_outbox.py b/bot_personal_outbox.py new file mode 100644 index 0000000..1adb596 --- /dev/null +++ b/bot_personal_outbox.py @@ -0,0 +1,350 @@ +# -*- coding: utf-8 -*- +"""Durable, failure-contained delivery queue for Bot Personal archives. + +The companion plugin owns the local write. A remote MemoryCompanion Bridge is +only a delivery target, so a missing or failing bridge cannot interrupt chat, +agenda maintenance, or proactive messaging. +""" +from __future__ import annotations + +import asyncio +from copy import deepcopy +from dataclasses import dataclass +from datetime import datetime, timezone +import hashlib +import json +from typing import Any, Awaitable, Callable, Mapping + +from .bot_personal_dto import BotPersonalArchiveDTO, build_bot_personal_dto + + +OUTBOX_STATES = frozenset({"pending", "retry", "sent", "deduplicated", "dead_letter", "invalid"}) + + +def _now_iso() -> str: + return datetime.now(timezone.utc).isoformat(timespec="seconds") + + +def _timestamp(value: Any, fallback: float | None = None) -> float: + if isinstance(value, (int, float)): + return float(value) + try: + return datetime.fromisoformat(str(value).replace("Z", "+00:00")).timestamp() + except (TypeError, ValueError, OverflowError): + return float(fallback if fallback is not None else 0.0) + + +def _fingerprint(envelope: Mapping[str, Any]) -> str: + canonical = json.dumps(dict(envelope), ensure_ascii=False, sort_keys=True, separators=(",", ":")) + return hashlib.sha256(canonical.encode("utf-8")).hexdigest() + + +@dataclass(frozen=True) +class OutboxResult: + ok: bool + state: str + idempotency_key: str + record_id: str = "" + deduplicated: bool = False + version: int = 0 + error_code: str | None = None + attempts: int = 0 + + def as_dict(self) -> dict[str, Any]: + return { + "ok": self.ok, + "state": self.state, + "idempotency_key": self.idempotency_key, + "record_id": self.record_id, + "deduplicated": self.deduplicated, + "version": self.version, + "error_code": self.error_code, + "attempts": self.attempts, + } + + +class BotPersonalOutbox: + """Persisted queue backed by ``plugin.data['bot_personal_outbox']``. + + ``clock`` returns wall-clock seconds and is injectable for deterministic + tests. ``sender`` is called only by :meth:`drain`; it should return the + structured Bridge result and must not raise for ordinary remote failures. + """ + + def __init__( + self, + data: dict[str, Any], + *, + save: Callable[[], Any] | None = None, + clock: Callable[[], float] | None = None, + max_attempts: int = 5, + base_backoff_seconds: float = 30.0, + max_backoff_seconds: float = 3600.0, + ) -> None: + self.data = data + self.save = save + self.clock = clock or __import__("time").time + self.max_attempts = max(1, int(max_attempts or 5)) + self.base_backoff_seconds = max(0.0, float(base_backoff_seconds or 30.0)) + self.max_backoff_seconds = max(self.base_backoff_seconds, float(max_backoff_seconds or 3600.0)) + self._lock = asyncio.Lock() + entries = self.data.setdefault("bot_personal_outbox", []) + if not isinstance(entries, list): + self.data["bot_personal_outbox"] = [] + + @property + def entries(self) -> list[dict[str, Any]]: + entries = self.data.get("bot_personal_outbox") + if not isinstance(entries, list): + entries = [] + self.data["bot_personal_outbox"] = entries + return [item for item in entries if isinstance(item, dict)] + + def _replace_entries(self, entries: list[dict[str, Any]]) -> None: + self.data["bot_personal_outbox"] = entries + + def _persist(self) -> None: + if not callable(self.save): + return + try: + result = self.save() + if asyncio.iscoroutine(result) or hasattr(result, "__await__"): + asyncio.create_task(result) + except Exception: + # The in-memory queue remains authoritative for this process; the + # next normal store save can still persist the already-mutated data. + return + + @staticmethod + def _key(memory_type: str, idempotency_key: str) -> str: + return f"{str(memory_type or '').strip()}::{str(idempotency_key or '').strip()}" + + def _find(self, entries: list[dict[str, Any]], memory_type: str, key: str) -> dict[str, Any] | None: + composite = self._key(memory_type, key) + for entry in entries: + if self._key(entry.get("memory_type"), entry.get("idempotency_key")) == composite: + return entry + return None + + def _normalise_sender_result(self, result: Any) -> dict[str, Any]: + if not isinstance(result, dict): + return {"ok": False, "state": "retry", "error_code": "invalid_bridge_response"} + normalized = dict(result) + normalized["ok"] = bool(result.get("ok")) + normalized["deduplicated"] = bool(result.get("deduplicated")) + normalized["version"] = int(result.get("version") or 0) + normalized["state"] = str(result.get("state") or ("sent" if normalized["ok"] else "retry")) + return normalized + + def _backoff(self, attempts: int) -> float: + exponent = max(0, int(attempts) - 1) + return min(self.max_backoff_seconds, self.base_backoff_seconds * (2 ** exponent)) + + async def enqueue( + self, + *, + memory_type: str, + payload: dict[str, Any], + idempotency_key: str, + occurred_at: str, + now: float | None = None, + version: int = 1, + source_refs: list[str] | None = None, + sender: Callable[[dict[str, Any]], Awaitable[dict[str, Any]]] | None = None, + **kwargs: Any, + ) -> dict[str, Any]: + """Validate, locally record, then optionally attempt immediate delivery.""" + try: + dto = build_bot_personal_dto( + memory_type=memory_type, + payload=payload, + idempotency_key=idempotency_key, + occurred_at=occurred_at, + version=version, + **kwargs, + ) + except Exception as exc: + return OutboxResult( + ok=False, + state="invalid", + idempotency_key=str(idempotency_key or ""), + error_code=getattr(exc, "error_code", "invalid"), + ).as_dict() + + envelope = dto.envelope() + if source_refs: + envelope["source_refs"] = list(dict.fromkeys(str(item).strip()[:240] for item in source_refs if str(item).strip())) + envelope_fingerprint = _fingerprint(envelope) + current = float(self.clock() if now is None else now) + async with self._lock: + entries = self.entries + existing = self._find(entries, dto.memory_type, dto.idempotency_key) + if existing is not None: + old_version = int(existing.get("version") or 0) + old_fingerprint = str(existing.get("payload_fingerprint") or "") + if old_version > dto.version: + return OutboxResult( + ok=False, + state="stale_version", + idempotency_key=dto.idempotency_key, + record_id=str(existing.get("record_id") or dto.record_id), + version=old_version, + error_code="stale_version", + attempts=int(existing.get("attempts") or 0), + ).as_dict() + if old_version == dto.version and old_fingerprint == envelope_fingerprint: + return OutboxResult( + ok=existing.get("state") in {"sent", "deduplicated"}, + state=str(existing.get("state") or "pending"), + idempotency_key=dto.idempotency_key, + record_id=str(existing.get("record_id") or dto.record_id), + deduplicated=True, + version=old_version, + error_code=existing.get("last_error"), + attempts=int(existing.get("attempts") or 0), + ).as_dict() + if old_version == dto.version and old_fingerprint != envelope_fingerprint: + return OutboxResult( + ok=False, + state="version_conflict", + idempotency_key=dto.idempotency_key, + record_id=str(existing.get("record_id") or dto.record_id), + version=old_version, + error_code="version_conflict", + attempts=int(existing.get("attempts") or 0), + ).as_dict() + existing.update({ + "envelope": deepcopy(envelope), + "payload_fingerprint": envelope_fingerprint, + "version": dto.version, + "state": "pending", + "attempts": 0, + "next_attempt_at": current, + "last_error": "", + "updated_at": _now_iso(), + "dead_letter_at": "", + }) + entry = existing + else: + entry = { + "outbox_id": f"outbox_{hashlib.sha1(self._key(dto.memory_type, dto.idempotency_key).encode()).hexdigest()[:20]}", + "memory_type": dto.memory_type, + "idempotency_key": dto.idempotency_key, + "record_id": dto.record_id, + "version": dto.version, + "envelope": deepcopy(envelope), + "payload_fingerprint": envelope_fingerprint, + "state": "pending", + "attempts": 0, + "next_attempt_at": current, + "last_error": "", + "created_at": _now_iso(), + "updated_at": _now_iso(), + "sent_at": "", + "dead_letter_at": "", + } + entries.append(entry) + self._replace_entries(entries) + self._persist() + + result = OutboxResult( + ok=False, + state="pending", + idempotency_key=dto.idempotency_key, + record_id=dto.record_id, + version=dto.version, + ).as_dict() + if callable(sender): + drained = await self.drain(sender, now=current, limit=1) + if drained: + return drained[0] + return result + + async def drain( + self, + sender: Callable[[dict[str, Any]], Awaitable[dict[str, Any]]], + *, + now: float | None = None, + limit: int = 16, + ) -> list[dict[str, Any]]: + """Try due entries once; retries are scheduled without sleeping.""" + current = float(self.clock() if now is None else now) + results: list[dict[str, Any]] = [] + for _ in range(max(0, int(limit or 0))): + async with self._lock: + due = [ + item for item in self.entries + if item.get("state") in {"pending", "retry"} + and _timestamp(item.get("next_attempt_at"), current) <= current + ] + if not due: + break + entry = due[0] + entry["attempts"] = int(entry.get("attempts") or 0) + 1 + entry["updated_at"] = _now_iso() + envelope = deepcopy(entry.get("envelope") or {}) + attempt = int(entry["attempts"]) + self._persist() + try: + response = self._normalise_sender_result(await sender(envelope)) + except Exception as exc: + response = {"ok": False, "state": "retry", "error_code": type(exc).__name__} + async with self._lock: + current_entry = self._find(self.entries, entry.get("memory_type", ""), entry.get("idempotency_key", "")) + if current_entry is None: + continue + current_entry["updated_at"] = _now_iso() + current_entry["last_error"] = str(response.get("error_code") or "")[:120] + current_entry["remote_record_id"] = str(response.get("record_id") or "")[:160] + current_entry["remote_version"] = int(response.get("version") or 0) + if response.get("ok"): + current_entry["state"] = "deduplicated" if response.get("deduplicated") else "sent" + current_entry["sent_at"] = _now_iso() + current_entry["next_attempt_at"] = 0 + elif response.get("state") in {"invalid", "version_conflict", "stale_version"}: + current_entry["state"] = "dead_letter" if response.get("state") != "invalid" else "invalid" + current_entry["dead_letter_at"] = _now_iso() + current_entry["next_attempt_at"] = 0 + elif attempt >= self.max_attempts: + current_entry["state"] = "dead_letter" + current_entry["dead_letter_at"] = _now_iso() + current_entry["next_attempt_at"] = 0 + else: + current_entry["state"] = "retry" + current_entry["next_attempt_at"] = current + self._backoff(attempt) + self._persist() + results.append({ + "ok": bool(response.get("ok")), + "state": current_entry["state"], + "idempotency_key": current_entry.get("idempotency_key", ""), + "record_id": current_entry.get("remote_record_id") or current_entry.get("record_id", ""), + "deduplicated": bool(response.get("deduplicated")), + "version": int(response.get("version") or current_entry.get("version") or 0), + "error_code": current_entry.get("last_error") or None, + "attempts": attempt, + }) + return results + + async def retry_dead_letter(self, idempotency_key: str, *, now: float | None = None) -> dict[str, Any]: + current = float(self.clock() if now is None else now) + async with self._lock: + for entry in self.entries: + if entry.get("idempotency_key") != idempotency_key: + continue + if entry.get("state") != "dead_letter": + return {"ok": False, "state": str(entry.get("state") or "pending"), "idempotency_key": idempotency_key} + entry.update({"state": "retry", "next_attempt_at": current, "last_error": "", "dead_letter_at": "", "updated_at": _now_iso()}) + self._persist() + return {"ok": True, "state": "retry", "idempotency_key": idempotency_key, "attempts": int(entry.get("attempts") or 0)} + return {"ok": False, "state": "missing", "idempotency_key": idempotency_key, "error_code": "not_found"} + + def status(self) -> dict[str, Any]: + entries = self.entries + counts = {state: 0 for state in OUTBOX_STATES} + for entry in entries: + state = str(entry.get("state") or "pending") + counts[state] = counts.get(state, 0) + 1 + return {"total": len(entries), "counts": counts, "pending": counts.get("pending", 0), "retry": counts.get("retry", 0), "sent": counts.get("sent", 0) + counts.get("deduplicated", 0), "dead_letter": counts.get("dead_letter", 0)} + + +__all__ = ["BotPersonalOutbox", "OutboxResult", "OUTBOX_STATES"] diff --git a/constants.py b/constants.py index 0fec42f..749108e 100644 --- a/constants.py +++ b/constants.py @@ -210,6 +210,10 @@ _DATA_STORE_KEYS = ( "users", "daily_plan", "daily_plan_history", + "agenda_version", + "observed_activities", + "window_snapshots", + "agenda_reconciliation_history", "daily_state", "state_conditions", "state_generated_day", @@ -242,6 +246,7 @@ _DATA_STORE_KEYS = ( "environment_change_awareness", "personal_goal_state", "food_menu", + "bot_personal_outbox", ) # 触发原因 - 真人化表达 diff --git a/core_store.py b/core_store.py index 267adeb..3ce6206 100644 --- a/core_store.py +++ b/core_store.py @@ -340,6 +340,10 @@ class CoreStoreMixin: "groups": {}, "daily_plan": {}, "daily_plan_history": [], + "agenda_version": 1, + "observed_activities": [], + "window_snapshots": [], + "agenda_reconciliation_history": [], "daily_state": {}, "state_conditions": [], "state_generated_day": "", @@ -358,6 +362,7 @@ class CoreStoreMixin: "recent_photo_continuity": {}, "daily_story_plan": {}, "daily_story_plan_history": [], + "bot_personal_outbox": [], "skill_growth": {}, "detail_enhanced_day": "", "detail_enhanced_segments": {}, @@ -403,6 +408,10 @@ class CoreStoreMixin: data.setdefault("groups", {}) data.setdefault("daily_plan", {}) data.setdefault("daily_plan_history", []) + data.setdefault("agenda_version", 1) + data.setdefault("observed_activities", []) + data.setdefault("window_snapshots", []) + data.setdefault("agenda_reconciliation_history", []) data.setdefault("daily_state", {}) data.setdefault("state_conditions", []) data.setdefault("state_generated_day", "") @@ -421,6 +430,7 @@ class CoreStoreMixin: data.setdefault("recent_photo_continuity", {}) data.setdefault("daily_story_plan", {}) data.setdefault("daily_story_plan_history", []) + data.setdefault("bot_personal_outbox", []) data.setdefault("skill_growth", {}) data.setdefault("detail_enhanced_day", "") data.setdefault("detail_enhanced_segments", {}) diff --git a/daily_state.py b/daily_state.py index 2a2d9d6..130fa21 100644 --- a/daily_state.py +++ b/daily_state.py @@ -480,6 +480,12 @@ class DailyStateMixin: if isinstance(story_plan, dict): self.data["daily_story_plan"] = story_plan self._save_data_sync() + diary_recorder = getattr(self, "_memory_companion_record_daily_diary", None) + if callable(diary_recorder): + try: + await diary_recorder(diary) + except Exception as exc: + logger.debug("[PrivateCompanion] Bot Personal 日记归档失败: %s", _single_line(exc, 160)) if memory_payload: try: await self._memory_companion_record_dream_fragment(**memory_payload) diff --git a/main.py b/main.py index a878b64..e552ddd 100644 --- a/main.py +++ b/main.py @@ -123,6 +123,7 @@ from .helpers import ( ) from .config_migration import migrate_flat_config_into_schema_groups from .bot_personal_contract import capability_descriptor, contract_self_check +from .bot_personal_outbox import BotPersonalOutbox from .person_context_contract import ( CONTRACT_NAME as PERSON_CONTRACT_NAME, CONTRACT_VERSION as PERSON_CONTRACT_VERSION, @@ -236,6 +237,7 @@ from .proactive_engine import ProactiveEngineMixin from .proactive_message import ProactiveMessageMixin from .proactive_chat_runtime_bridge import ProactiveChatRuntimeBridge from .daily_state import DailyStateMixin +from .agenda_runtime import AgendaRuntimeMixin from .scene_context import SceneContextMixin from .state_views import StateViewsMixin from .interaction_utils import InteractionUtilsMixin @@ -999,6 +1001,7 @@ class PrivateCompanionPlugin( SceneContextMixin, ProactiveMessageMixin, DailyStateMixin, + AgendaRuntimeMixin, StateViewsMixin, InteractionUtilsMixin, LlmToolActionsMixin, @@ -2216,6 +2219,10 @@ class PrivateCompanionPlugin( self._qzone_last_bot = None startup_load_started = time.perf_counter() self.data = self._load_data_sync() + self._bot_personal_outbox = BotPersonalOutbox( + self.data, + save=lambda: self._schedule_data_save(delay=0.5), + ) self.unified_person_registry = UnifiedPersonRegistry(self.data) self._apply_tts_runtime_overrides() load_elapsed_ms = int((time.perf_counter() - startup_load_started) * 1000) @@ -2618,6 +2625,10 @@ class PrivateCompanionPlugin( self._install_send_message_to_user_tool_sanitizer() self._schedule_default_persona_prompt_refresh() needs_startup_save = False + agenda_before = bool(getattr(self, "_agenda_migration_dirty", False)) + self._agenda_prepare_store() + if getattr(self, "_agenda_migration_dirty", False) and not agenda_before: + needs_startup_save = True async with self._data_lock: changed = False raw_users = self.data.get("users") if isinstance(self.data, dict) else None @@ -11415,6 +11426,14 @@ wakeup_type={_single_line(wakeup.get('type'), 40)} score={_single_line(wakeup.ge "disabled" if existing_user_disabled else "not_target", ) return + self._record_c3_inbound_activity( + event, + text=text, + received_ts=received_ts, + user_id=canonical_user_id or user_id, + sender_id=user_id, + sender_name=sender_display_name, + ) await self._cancel_activity_followup_on_user_return( canonical_user_id or user_id, trigger_message_id=self._event_message_id(event), @@ -12136,6 +12155,61 @@ wakeup_type={_single_line(wakeup.get('type'), 40)} score={_single_line(wakeup.ge asyncio.create_task(self._maybe_refresh_companion_memory(user_id, user_snapshot)) asyncio.create_task(self._maybe_refresh_dialogue_episode(user_id, user_snapshot)) + def _record_c3_inbound_activity( + self, + event: AstrMessageEvent, + *, + text: str, + received_ts: float, + user_id: str = "", + group_id: str = "", + sender_id: str = "", + sender_name: str = "", + ) -> dict[str, Any] | None: + """Feed the C3 local activity aggregator without changing the reply path.""" + if not _single_line(text, 400): + return None + capture = getattr(self, "_agenda_capture_inbound_message", None) + if not callable(capture): + return None + scope = "group" if _single_line(group_id, 80) else "private" + subject_id = _single_line(group_id or user_id, 120) + conversation_id = f"{scope}:{subject_id}" if subject_id else scope + source_ref = _single_line(self._event_message_id(event), 160) + if not source_ref: + source_ref = _single_line(getattr(event, "unified_msg_origin", ""), 180) + if not source_ref: + source_ref = f"{conversation_id}:{int(received_ts)}" + try: + event_time = self._environment_fromtimestamp(received_ts) + except Exception: + event_time = datetime.fromtimestamp(received_ts).astimezone() + try: + result = capture( + text=_single_line(text, 400), + event_time=event_time, + source_ref=source_ref, + conversation_id=conversation_id, + participant=_single_line(sender_name or sender_id or "user", 120), + message_count=1, + visibility="group" if scope == "group" else "private", + ) + if isinstance(result, dict): + result["scope"] = scope + if result is not None and scope == "private": + recorder = getattr(self, "_memory_companion_record_observed_activity", None) + if callable(recorder): + asyncio.create_task(recorder(result)) + return result + except Exception as exc: + logger.debug( + "[PrivateCompanion] C3 活动捕获失败,已放行原消息: scope=%s id=%s error=%s", + scope, + subject_id or "-", + _single_line(exc, 160), + ) + return None + async def _capture_group_observation_event( self, event: AstrMessageEvent, @@ -12248,6 +12322,14 @@ wakeup_type={_single_line(wakeup.get('type'), 40)} score={_single_line(wakeup.ge sender_name=self._sender_display_name(event), text=text, ) + self._record_c3_inbound_activity( + event, + text=text, + received_ts=_now_ts(), + group_id=group_id, + sender_id=sender_id, + sender_name=self._sender_display_name(event), + ) self._start_group_image_understanding( event, group_id=group_id, diff --git a/memory_companion_adapter.py b/memory_companion_adapter.py index be27aaf..500ac29 100644 --- a/memory_companion_adapter.py +++ b/memory_companion_adapter.py @@ -19,7 +19,9 @@ from .bot_personal_contract import ( CONTRACT_FINGERPRINT, CONTRACT_REVISION, WINDOW_SLUGS, + window_for_minutes, ) +from .bot_personal_outbox import BotPersonalOutbox from .helpers import _missing_optional_model_dependency, _path_text, _safe_float, _safe_int, _single_line @@ -168,6 +170,140 @@ class MemoryCompanionAdapterMixin: return bridge return None + def _memory_companion_outbox(self) -> BotPersonalOutbox | None: + current = getattr(self, "_bot_personal_outbox", None) + if isinstance(current, BotPersonalOutbox): + return current + data = getattr(self, "data", None) + if not isinstance(data, dict): + return None + try: + current = BotPersonalOutbox( + data, + save=lambda: self._schedule_data_save(delay=0.5), + ) + except Exception as exc: + logger.debug("[PrivateCompanion] Bot Personal outbox 初始化失败: %s", _single_line(exc, 120)) + return None + try: + setattr(self, "_bot_personal_outbox", current) + except Exception: + pass + return current + + def _memory_companion_bot_personal_sender(self) -> Any | None: + bridge = self._memory_companion_bridge() + recorder = getattr(bridge, "record_bot_personal_archive", None) if bridge is not None else None + if not callable(recorder): + return None + + async def _send(envelope: dict[str, Any]) -> dict[str, Any]: + result = recorder(envelope) + if asyncio.iscoroutine(result) or hasattr(result, "__await__"): + result = await result + return result if isinstance(result, dict) else {"ok": False, "state": "retry", "error_code": "invalid_bridge_response"} + + return _send + + async def _memory_companion_record_bot_personal( + self, + *, + memory_type: str, + payload: dict[str, Any], + idempotency_key: str, + occurred_at: str = "", + version: int = 1, + source_refs: list[str] | None = None, + ) -> dict[str, Any]: + outbox = self._memory_companion_outbox() + if outbox is None: + return { + "ok": False, + "state": "local_only", + "record_id": "", + "deduplicated": False, + "version": int(version or 1), + "error_code": "outbox_unavailable", + } + try: + result = await outbox.enqueue( + memory_type=memory_type, + payload=payload, + idempotency_key=idempotency_key, + occurred_at=occurred_at or self._memory_companion_now_iso(), + version=max(1, int(version or 1)), + source_refs=source_refs, + sender=self._memory_companion_bot_personal_sender(), + ) + self._bridge_last_status = { + **getattr(self, "_bridge_last_status", {}), + "bot_personal_outbox": outbox.status(), + } + return result + except Exception as exc: + logger.debug("[PrivateCompanion] Bot Personal 本地归档失败: %s", _single_line(exc, 160)) + return { + "ok": False, + "state": "local_only", + "record_id": "", + "deduplicated": False, + "version": int(version or 1), + "error_code": "outbox_enqueue_failed", + } + + async def _memory_companion_flush_bot_personal_outbox(self, *, limit: int = 16) -> list[dict[str, Any]]: + outbox = self._memory_companion_outbox() + sender = self._memory_companion_bot_personal_sender() + if outbox is None or sender is None: + return [] + try: + results = await outbox.drain(sender, limit=max(1, int(limit or 16))) + self._bridge_last_status = { + **getattr(self, "_bridge_last_status", {}), + "bot_personal_outbox": outbox.status(), + } + return results + except Exception as exc: + logger.debug("[PrivateCompanion] Bot Personal outbox 补投失败: %s", _single_line(exc, 160)) + return [] + + async def _memory_companion_record_observed_activity(self, activity: dict[str, Any]) -> dict[str, Any]: + """Archive only private observed activity; group observations stay local/group-scoped.""" + if not isinstance(activity, dict) or _single_line(activity.get("visibility"), 32) != "private": + return {"ok": False, "state": "local_only", "error_code": "non_private_activity"} + activity_id = _single_line(activity.get("activity_id"), 160) + title = _single_line(activity.get("title") or activity.get("summary"), 180) + if not activity_id or not title: + return {"ok": False, "state": "invalid", "error_code": "invalid_activity"} + payload = { + "date": _single_line(activity.get("start_at"), 10), + "window": window_for_minutes(0), + "summary": title, + "activity_id": activity_id, + "kind": _single_line(activity.get("kind"), 48), + "participants": [_single_line(item, 80) for item in (activity.get("participants") or []) if _single_line(item, 80)][:8], + "message_count": int(activity.get("message_count") or len(activity.get("source_refs") or []) or 1), + } + try: + occurred_at = _single_line(activity.get("start_at"), 80) or self._memory_companion_now_iso() + if "+" in occurred_at or occurred_at.endswith("Z"): + parsed = occurred_at.replace("Z", "+00:00") + from datetime import datetime + + moment = datetime.fromisoformat(parsed) + payload["window"] = window_for_minutes(moment.hour * 60 + moment.minute) + except Exception: + occurred_at = self._memory_companion_now_iso() + source_refs = [_single_line(item, 160) for item in (activity.get("source_refs") or []) if _single_line(item, 160)] + return await self._memory_companion_record_bot_personal( + memory_type="bot_observed_activity", + payload=payload, + idempotency_key=f"observed:{activity_id}", + occurred_at=occurred_at, + version=int(activity.get("version") or 1), + source_refs=source_refs or [f"companion:observed:{activity_id}"], + ) + def _memory_companion_bridge_from_module(self, module: Any | None) -> Any | None: module_vars = getattr(module, "__dict__", {}) if module is not None else {} getter = module_vars.get("get_active_bridge") if isinstance(module_vars, dict) else None @@ -823,13 +959,108 @@ class MemoryCompanionAdapterMixin: "只在与本轮直接相关时自然接住;不要主动列举记忆、不要提及检索过程,也不要把它当作其他用户的信息。" ) + async def _memory_companion_record_agenda_snapshot(self, snapshot: dict[str, Any]) -> dict[str, Any]: + if not isinstance(snapshot, dict): + return {"ok": False, "state": "invalid", "error_code": "invalid_snapshot"} + date_text = _single_line(snapshot.get("window_date") or snapshot.get("date"), 20) + window = _single_line(snapshot.get("window") or snapshot.get("slug"), 32) + snapshot_id = _single_line(snapshot.get("snapshot_id"), 160) or f"agenda_snapshot:{date_text}:{window}" + + def _compact(items: Any, field: str) -> list[dict[str, Any]]: + result: list[dict[str, Any]] = [] + for item in items if isinstance(items, list) else []: + if not isinstance(item, dict): + continue + value = _single_line(item.get("title") or item.get("summary") or item.get(field), 180) + if not value: + continue + result.append({ + "id": _single_line(item.get("plan_id") or item.get("activity_id") or item.get("entry_id"), 120), + "summary": value, + "status": _single_line(item.get("status"), 32), + }) + return result[:16] + + payload = { + "date": date_text, + "window": window, + "summary": f"{date_text} {window} 窗口快照", + "planned": _compact(snapshot.get("planned"), "title"), + "observed": _compact(snapshot.get("observed"), "summary"), + "reconciled": _compact(snapshot.get("reconciled"), "reason"), + "open_items": [_single_line(item, 160) for item in (snapshot.get("open_items") or []) if _single_line(item, 160)][:12], + } + refs = [snapshot_id] + refs.extend(_single_line(item, 160) for item in (snapshot.get("source_refs") or []) if _single_line(item, 160)) + return await self._memory_companion_record_bot_personal( + memory_type="bot_window_snapshot", + payload=payload, + idempotency_key=snapshot_id, + occurred_at=_single_line(snapshot.get("generated_at"), 80) or self._memory_companion_now_iso(), + version=int(snapshot.get("version") or 1), + source_refs=list(dict.fromkeys(refs)), + ) + + async def _memory_companion_record_agenda_reconciliation(self, reconciliation: dict[str, Any]) -> dict[str, Any]: + if not isinstance(reconciliation, dict): + return {"ok": False, "state": "invalid", "error_code": "invalid_reconciliation"} + date_text = _single_line(reconciliation.get("window_date") or reconciliation.get("date"), 20) + window = _single_line(reconciliation.get("window") or reconciliation.get("slug"), 32) + record_id = _single_line(reconciliation.get("reconciliation_id"), 160) or f"reconciliation:{date_text}:{window}" + plans = [] + for item in reconciliation.get("plans") if isinstance(reconciliation.get("plans"), list) else []: + if not isinstance(item, dict): + continue + plans.append({ + "plan_id": _single_line(item.get("plan_id"), 120), + "status": _single_line(item.get("status"), 32), + "reason": _single_line(item.get("reason") or item.get("reconciliation_reason"), 180), + "activity_ids": [_single_line(value, 120) for value in (item.get("activity_ids") or item.get("reconciled_activity_ids") or []) if _single_line(value, 120)][:12], + }) + payload = { + "date": date_text, + "window": window, + "summary": f"{date_text} {window} 计划与实际对账", + "plans": plans[:16], + "observed_activity_ids": [_single_line(value, 120) for value in (reconciliation.get("observed_activity_ids") or []) if _single_line(value, 120)][:16], + } + refs = [record_id] + refs.extend(_single_line(item, 160) for item in (reconciliation.get("source_refs") or []) if _single_line(item, 160)) + return await self._memory_companion_record_bot_personal( + memory_type="bot_schedule_reconciliation", + payload=payload, + idempotency_key=record_id, + occurred_at=_single_line(reconciliation.get("generated_at"), 80) or self._memory_companion_now_iso(), + version=int(reconciliation.get("version") or 1), + source_refs=list(dict.fromkeys(refs)), + ) + + async def _memory_companion_record_daily_diary(self, diary: dict[str, Any]) -> dict[str, Any]: + if not isinstance(diary, dict): + return {"ok": False, "state": "invalid", "error_code": "invalid_diary"} + date_text = _single_line(diary.get("date"), 20) + summary = _single_line(diary.get("summary") or diary.get("share_seed") or diary.get("body"), 360) + if not date_text or not summary: + return {"ok": False, "state": "invalid", "error_code": "empty_diary"} + payload = { + "date": date_text, + "summary": summary, + "mood": _single_line(diary.get("mood") or diary.get("emotion"), 60), + "tags": [_single_line(item, 60) for item in (diary.get("tags") or []) if _single_line(item, 60)][:12], + "dream_summary": _single_line(diary.get("dream_summary") or diary.get("dream"), 160), + } + return await self._memory_companion_record_bot_personal( + memory_type="bot_daily_diary", + payload=payload, + idempotency_key=f"diary:{date_text}", + occurred_at=self._memory_companion_now_iso(), + version=int(diary.get("version") or 1), + source_refs=[f"companion:diary:{date_text}"], + ) + async def _memory_companion_record_daily_plan(self, plan: dict[str, Any]) -> None: if not isinstance(plan, dict): return - bridge = self._memory_companion_bridge() - recorder = getattr(bridge, "record_schedule_fragment", None) if bridge is not None else None - if not callable(recorder): - return date_text = _single_line(plan.get("date"), 40) items = plan.get("items") if not date_text or not isinstance(items, list) or not items: @@ -855,6 +1086,30 @@ class MemoryCompanionAdapterMixin: lines.append(line) if not lines: return + try: + now = self._environment_now() + window = window_for_minutes(now.hour * 60 + now.minute) + except Exception: + window = "" + await self._memory_companion_record_bot_personal( + memory_type="bot_schedule_plan", + payload={ + "date": date_text, + "window": window, + "summary": f"{date_text} 的 Bot 当日生活日程已生成", + "items": lines, + "source": _single_line(plan.get("source"), 40), + "item_count": len(lines), + }, + idempotency_key=f"daily_plan:{date_text}", + occurred_at=_single_line(plan.get("generated_at"), 80) or self._memory_companion_now_iso(), + version=int(plan.get("version") or 1), + source_refs=[f"companion:daily_plan:{date_text}"], + ) + bridge = self._memory_companion_bridge() + recorder = getattr(bridge, "record_schedule_fragment", None) if bridge is not None else None + if not callable(recorder): + return content = f"{date_text} 的 Bot 当日生活日程已生成:\n" + "\n".join(f"- {line}" for line in lines) try: await recorder( @@ -887,10 +1142,6 @@ class MemoryCompanionAdapterMixin: ) -> None: if not isinstance(segment, dict) or not isinstance(detail, dict): return - bridge = self._memory_companion_bridge() - recorder = getattr(bridge, "record_schedule_fragment", None) if bridge is not None else None - if not callable(recorder): - return date_text = _single_line(plan.get("date") if isinstance(plan, dict) else "", 40) start = 0 end = 0 @@ -925,6 +1176,25 @@ class MemoryCompanionAdapterMixin: proactive.append(text) if not summary and not events and not proactive: return + await self._memory_companion_record_bot_personal( + memory_type="bot_detail_fragment", + payload={ + "date": date_text, + "window": window_for_minutes(start % (24 * 60)), + "summary": summary or "日程细化", + "events": events[:4], + "proactive_events": proactive[:3], + "start": start_text, + "end": end_text, + }, + idempotency_key=f"detail:{date_text}:{start}:{end}", + occurred_at=self._memory_companion_now_iso(), + source_refs=[f"companion:detail:{date_text}:{start}:{end}"], + ) + bridge = self._memory_companion_bridge() + recorder = getattr(bridge, "record_schedule_fragment", None) if bridge is not None else None + if not callable(recorder): + return parts = [f"{date_text} {start_text}-{end_text} 的 Bot 日程细化:"] if summary: parts.append(summary) diff --git a/proactive.py b/proactive.py index 97ae3a0..f374d76 100644 --- a/proactive.py +++ b/proactive.py @@ -10,6 +10,7 @@ import gc import hashlib import html import importlib +import inspect import json import math import os @@ -2917,6 +2918,47 @@ class ProactiveMixin: if isinstance(state, dict): state.pop(label, None) + async def _run_agenda_maintenance_tick(self) -> list[dict[str, Any]]: + """Settle local windows, archive compact projections, then drain outbox.""" + tick = getattr(self, "_agenda_maintenance_tick", None) + settled: Any = [] + if callable(tick): + settled = tick() + if inspect.isawaitable(settled): + settled = await settled + snapshots = [item for item in settled if isinstance(item, dict)] if isinstance(settled, list) else [] + snapshot_recorder = getattr(self, "_memory_companion_record_agenda_snapshot", None) + reconciliation_recorder = getattr(self, "_memory_companion_record_agenda_reconciliation", None) + history = self.data.get("agenda_reconciliation_history") if isinstance(getattr(self, "data", None), dict) else [] + for snapshot in snapshots: + if callable(snapshot_recorder): + try: + await snapshot_recorder(snapshot) + except Exception as exc: + logger.debug("[PrivateCompanion] C3 窗口快照归档失败: %s", _single_line(exc, 160)) + if callable(reconciliation_recorder) and isinstance(history, list): + snapshot_id = _single_line(snapshot.get("snapshot_id"), 160) + for reconciliation in reversed(history): + if not isinstance(reconciliation, dict): + continue + refs = reconciliation.get("source_refs") if isinstance(reconciliation.get("source_refs"), list) else [] + if snapshot_id and snapshot_id not in refs: + continue + try: + await reconciliation_recorder(reconciliation) + except Exception as exc: + logger.debug("[PrivateCompanion] C3 对账归档失败: %s", _single_line(exc, 160)) + break + flusher = getattr(self, "_memory_companion_flush_bot_personal_outbox", None) + if callable(flusher): + try: + await flusher(limit=24) + except Exception as exc: + logger.debug("[PrivateCompanion] C3 Bot Personal outbox 补投失败: %s", _single_line(exc, 160)) + if snapshots and callable(getattr(self, "_schedule_data_save", None)): + self._schedule_data_save(delay=0.5) + return snapshots + async def _scheduler_loop(self): while not self._stop_event.is_set(): try: @@ -2929,6 +2971,7 @@ class ProactiveMixin: maintenance_tasks = ( ("日常状态", self._ensure_daily_state), ("今日日程", self._ensure_daily_plan), + ("日程归档", self._run_agenda_maintenance_tick), ("当前细化", self._ensure_detail_enhancement), ("当前在线感", self._ensure_current_detail_presence_status), ("日记", self._ensure_daily_diary), @@ -2941,7 +2984,7 @@ class ProactiveMixin: ("被动注入缓存", self._refresh_passive_injection_cache), ) if self._proactive_generation_disabled(): - passive_labels = {"日常状态", "今日日程", "当前细化", "当前在线感", "日记", "被动注入缓存"} + passive_labels = {"日常状态", "今日日程", "日程归档", "当前细化", "当前在线感", "日记", "被动注入缓存"} maintenance_tasks = tuple(item for item in maintenance_tasks if item[0] in passive_labels) for label, task_factory in maintenance_tasks: try: @@ -2963,6 +3006,7 @@ class ProactiveMixin: maintenance_tasks = ( ("日常状态", self._ensure_daily_state), ("今日日程", self._ensure_daily_plan), + ("日程归档", self._run_agenda_maintenance_tick), ("当前细化", self._ensure_detail_enhancement), ("当前在线感", self._ensure_current_detail_presence_status), ("日记", self._ensure_daily_diary), @@ -2975,7 +3019,7 @@ class ProactiveMixin: ("被动注入缓存", self._refresh_passive_injection_cache), ) if self._proactive_generation_disabled(): - passive_labels = {"日常状态", "今日日程", "当前细化", "当前在线感", "日记", "被动注入缓存"} + passive_labels = {"日常状态", "今日日程", "日程归档", "当前细化", "当前在线感", "日记", "被动注入缓存"} maintenance_tasks = tuple(item for item in maintenance_tasks if item[0] in passive_labels) for label, task_factory in maintenance_tasks: try: diff --git a/schedule_reconciler.py b/schedule_reconciler.py new file mode 100644 index 0000000..a274752 --- /dev/null +++ b/schedule_reconciler.py @@ -0,0 +1,162 @@ +# -*- coding: utf-8 -*- +"""Deterministic plan/actual reconciliation for the local C3 agenda.""" +from __future__ import annotations + +from datetime import datetime, timedelta +import re +from typing import Any + +try: + from .agenda_contracts import ( + agenda_entry_from_activity, + agenda_entry_from_plan, + interval_overlaps_window, + normalize_observed_activity, + normalize_plan_item, + parse_datetime, + stable_id, + ) +except ImportError: + from agenda_contracts import ( + agenda_entry_from_activity, + agenda_entry_from_plan, + interval_overlaps_window, + normalize_observed_activity, + normalize_plan_item, + parse_datetime, + stable_id, + ) + + +def _normalized_text(value: Any) -> str: + return re.sub(r"[^0-9a-z\u4e00-\u9fff]+", "", str(value or "").lower()) + + +def _tokens(value: Any) -> set[str]: + text = _normalized_text(value) + if not text: + return set() + tokens = {text} + tokens.update(text[index:index + 2] for index in range(max(0, len(text) - 1))) + tokens.update(text[index:index + 3] for index in range(max(0, len(text) - 2))) + return {token for token in tokens if len(token) >= 2} + + +def _interval(item: dict[str, Any], now: datetime) -> tuple[datetime | None, datetime | None]: + try: + start = parse_datetime(item.get("start_at") or item.get("start"), default=now) + except Exception: + return None, None + try: + end = parse_datetime(item.get("end_at") or item.get("end"), default=start) + except Exception: + end = start + if end <= start: + end = start + timedelta(seconds=1) + return start, end + + +def _overlaps(plan: dict[str, Any], activity: dict[str, Any], now: datetime) -> bool: + ps, pe = _interval(plan, now) + a_s, a_e = _interval(activity, now) + return bool(ps and pe and a_s and a_e and ps < a_e and a_s < pe) + + +def _title_similarity(plan: dict[str, Any], activity: dict[str, Any]) -> float: + plan_text = _normalized_text(plan.get("title") or plan.get("activity")) + activity_text = _normalized_text(activity.get("title") or activity.get("summary")) + if not plan_text or not activity_text: + return 0.0 + if plan_text in activity_text or activity_text in plan_text: + return 1.0 + shared = _tokens(plan_text).intersection(_tokens(activity_text)) + if not shared: + return 0.0 + return len(shared) / max(1, len(_tokens(plan_text).union(_tokens(activity_text)))) + + +def _same_source(plan: dict[str, Any], activity: dict[str, Any]) -> bool: + plan_id = str(plan.get("plan_id") or plan.get("event_id") or "").strip() + plan_refs = {str(ref) for ref in (plan.get("source_refs") or []) if str(ref)} + activity_refs = {str(ref) for ref in (activity.get("source_refs") or []) if str(ref)} + return bool((plan_id and plan_id in activity_refs) or plan_refs.intersection(activity_refs)) + + +def _plan_is_past(plan: dict[str, Any], now: datetime) -> bool: + _start, end = _interval(plan, now) + return bool(end and end <= now) + + +def reconcile( + plans: list[dict[str, Any]], + activities: list[dict[str, Any]], + *, + now: datetime, +) -> dict[str, Any]: + normalized_plans: list[dict[str, Any]] = [] + for raw in plans or []: + try: + normalized_plans.append(normalize_plan_item(raw, now=now)) + except Exception: + continue + normalized_activities: list[dict[str, Any]] = [] + for raw in activities or []: + try: + normalized_activities.append(normalize_observed_activity(raw, now=now)) + except Exception: + continue + + used_activity_ids: set[str] = set() + reconciled_plans: list[dict[str, Any]] = [] + reconciliations: list[dict[str, Any]] = [] + entries: list[dict[str, Any]] = [] + matched: dict[str, list[str]] = {} + + for plan in normalized_plans: + plan_id = str(plan.get("plan_id") or stable_id("plan", plan.get("title"))) + exact = [activity for activity in normalized_activities if _same_source(plan, activity)] + overlap_candidates = [activity for activity in normalized_activities if _overlaps(plan, activity, now)] + similar = [activity for activity in overlap_candidates if _title_similarity(plan, activity) >= 0.18] + selected = exact or similar + plan_result = dict(plan) + if selected: + ids = [str(item.get("activity_id")) for item in selected] + matched[plan_id] = ids + used_activity_ids.update(ids) + all_completed = all(str(item.get("status")) == "completed" for item in selected) + plan_result["status"] = "completed" if all_completed else "active" + reason = "plan_id/source_refs matched" if exact else "time overlap and controlled title similarity" + plan_result["reconciliation_reason"] = reason + plan_result["reconciled_activity_ids"] = ids + reconciliation = { + "reconciliation_id": stable_id("reconciliation", plan_id, ids), + "plan_id": plan_id, + "status": plan_result["status"], + "source_kind": "reconciled", + "evidence_level": "L3" if any(item.get("evidence_level") in {"L3", "L4", "L5"} for item in selected) else "L2", + "source_refs": [plan_id, *ids], + "activity_ids": ids, + "reason": reason, + } + reconciliations.append(reconciliation) + entries.append(agenda_entry_from_plan(plan_result, reason=reason)) + entries.extend(agenda_entry_from_activity(item, source_refs=[plan_id, *ids]) for item in selected) + else: + if plan_result.get("status") not in {"cancelled", "deferred"} and _plan_is_past(plan_result, now): + plan_result["status"] = "unknown" + plan_result["reconciliation_reason"] = "window ended without observed evidence" + entries.append(agenda_entry_from_plan(plan_result, reason=plan_result.get("reconciliation_reason", ""))) + reconciled_plans.append(plan_result) + + for activity in normalized_activities: + if str(activity.get("activity_id")) not in used_activity_ids: + entries.append(agenda_entry_from_activity(activity)) + + entries.sort(key=lambda item: (str(item.get("start_at") or ""), 0 if item.get("kind") == "observed" else 1)) + return { + "plans": reconciled_plans, + "activities": normalized_activities, + "entries": entries, + "matched": matched, + "reconciliations": reconciliations, + } diff --git a/tests/test_c3_agenda.py b/tests/test_c3_agenda.py new file mode 100644 index 0000000..23919a6 --- /dev/null +++ b/tests/test_c3_agenda.py @@ -0,0 +1,177 @@ +# -*- coding: utf-8 -*- +from __future__ import annotations + +from datetime import datetime +import json +import sys +from pathlib import Path + +import pytest + +PACKAGE_DIR = Path(__file__).resolve().parents[1] +if str(PACKAGE_DIR) not in sys.path: + sys.path.insert(0, str(PACKAGE_DIR)) + +from activity_capture import ActivityCapture +from agenda_contracts import ( + SCHEDULE_WINDOWS, + interval_overlaps_window, + migrate_store, + normalize_observed_activity, + normalize_plan_item, + stable_id, + window_bounds, + window_for_datetime, + window_for_minutes, + window_for_plan_minutes, +) +from agenda_runtime import AgendaRuntimeMixin +from schedule_reconciler import reconcile +from unified_agenda import format_agenda_context + + +def dt(value: str) -> datetime: + return datetime.fromisoformat(value) + + +def test_contract_has_five_complete_windows_and_all_boundaries() -> None: + assert len(SCHEDULE_WINDOWS) == 5 + assert sum((end - start) % 1440 for _slug, _name, start, end in SCHEDULE_WINDOWS) == 1440 + expected = { + 0: "late_night", + 30: "late_night", + 5 * 60 + 59: "late_night", + 6 * 60: "morning", + 11 * 60: "noon", + 14 * 60 + 30: "afternoon", + 18 * 60: "evening", + 21 * 60: "late_night", + } + for minute, slug in expected.items(): + assert window_for_minutes(minute) == slug + + +@pytest.mark.parametrize( + ("value", "slug", "window_date"), + [ + ("2026-07-30T21:00:00+08:00", "late_night", "2026-07-30"), + ("2026-07-30T00:30:00+08:00", "late_night", "2026-07-29"), + ("2026-07-30T05:59:00+08:00", "late_night", "2026-07-29"), + ("2026-07-30T06:00:00+08:00", "morning", "2026-07-30"), + ("2026-07-30T11:00:00+08:00", "noon", "2026-07-30"), + ("2026-07-30T14:30:00+08:00", "afternoon", "2026-07-30"), + ("2026-07-30T18:00:00+08:00", "evening", "2026-07-30"), + ("2026-07-30T21:00:00+08:00", "late_night", "2026-07-30"), + ], +) +def test_datetime_boundaries_and_cross_midnight(value: str, slug: str, window_date: str) -> None: + actual_slug, actual_date, start, end = window_for_datetime(dt(value)) + assert (actual_slug, actual_date) == (slug, window_date) + assert start < dt(value) < end or start == dt(value) + assert (end - start).total_seconds() == (9 * 60 * 60 if slug == "late_night" else {"morning": 5, "noon": 3.5, "afternoon": 3.5, "evening": 3}[slug] * 3600) + + +def test_plan_minutes_and_interval_overlap_are_cross_midnight_safe() -> None: + assert window_for_plan_minutes("2026-07-30", 30) == ("late_night", "2026-07-29") + assert window_for_plan_minutes("2026-07-30", 21 * 60) == ("late_night", "2026-07-30") + start, end = window_bounds("2026-07-29", "late_night") + assert interval_overlaps_window({"start_at": "2026-07-30T00:30:00+08:00", "end_at": "2026-07-30T01:00:00+08:00"}, start, end) + assert not interval_overlaps_window({"start_at": "2026-07-30T06:00:00+08:00", "end_at": "2026-07-30T06:10:00+08:00"}, start, end) + + +def test_activity_capture_aggregates_by_conversation_bucket_and_deduplicates_refs() -> None: + capture = ActivityCapture(window_minutes=30, min_sustained_messages=3) + base = dt("2026-07-30T10:01:00+08:00") + assert capture.capture_message(text="我们继续开发插件", event_time=base, source_ref="m1", conversation_id="c1") is None + assert capture.capture_message(text="继续排查测试", event_time=base.replace(minute=10), source_ref="m2", conversation_id="c1") is None + activity = capture.capture_message(text="已经跑完测试", event_time=base.replace(minute=20), source_ref="m3", conversation_id="c1", participant="user") + assert activity is not None + assert activity["source_refs"] == ["m1", "m2", "m3"] + assert set(activity["participants"]) == {"user", "bot"} + same = capture.capture_message(text="重复投递", event_time=base.replace(minute=20), source_ref="m3", conversation_id="c1") + assert same is not None + assert same["activity_id"] == activity["activity_id"] + assert same["source_refs"] == activity["source_refs"] + + +def test_hard_fact_keeps_evidence_visibility_and_source_refs() -> None: + fact = ActivityCapture().capture_hard_fact( + title="运行工具", + start_at=dt("2026-07-30T12:00:00+08:00"), + end_at=dt("2026-07-30T12:05:00+08:00"), + source="tool", + source_refs=["tool-1"], + participants=["bot"], + visibility="private", + ) + assert fact["source_kind"] == "observed" + assert fact["evidence_level"] == "L3" + assert fact["visibility"] == "private" + assert fact["source_refs"] == ["tool-1"] + + +def test_reconcile_prioritizes_refs_and_marks_past_plan_unknown_without_evidence() -> None: + plans = [ + normalize_plan_item({"plan_id": "p1", "title": "开发插件", "start_at": "2026-07-30T10:00:00+08:00", "end_at": "2026-07-30T11:00:00+08:00", "source_refs": ["ticket-1"]}), + normalize_plan_item({"plan_id": "p2", "title": "午休", "start_at": "2026-07-30T12:00:00+08:00", "end_at": "2026-07-30T13:00:00+08:00"}), + ] + activities = [ + normalize_observed_activity({"title": "实际开发插件", "start_at": "2026-07-30T10:10:00+08:00", "end_at": "2026-07-30T10:30:00+08:00", "source_refs": ["ticket-1"]}), + ] + result = reconcile(plans, activities, now=dt("2026-07-30T14:00:00+08:00")) + by_id = {item["plan_id"]: item for item in result["plans"]} + assert by_id["p1"]["status"] == "active" + assert by_id["p1"]["reconciled_activity_ids"] + assert by_id["p2"]["status"] == "unknown" + assert all(item.get("source_kind") != "observed" for item in result["plans"]) + + +def test_normalizers_preserve_contract_fields_and_stable_id() -> None: + raw = {"title": "x", "status": "completed", "version": 4, "source_refs": ["a"], "visibility": "private", "certainty": "high", "evidence_level": "L3"} + normalized = normalize_plan_item(raw) + assert normalized["source_kind"] == "planned" + assert normalized["status"] == "completed" + assert normalized["version"] == 4 + assert normalized["source_refs"] == ["a"] + assert stable_id("x", {"b": 2, "a": 1}) == stable_id("x", {"a": 1, "b": 2}) + json.dumps(normalized, ensure_ascii=False) + + +class Host(AgendaRuntimeMixin): + def __init__(self, data: dict | None = None): + self.data = data or {} + self.calendar_timezone = "Asia/Shanghai" + self.fixed_now = dt("2026-07-30T22:00:00+08:00") + + def _calendar_now(self) -> datetime: + return self.fixed_now + + +def test_runtime_snapshot_is_idempotent_and_context_is_formatted() -> None: + host = Host({"daily_plan": {"date": "2026-07-30", "items": [{"time": "21:00", "end": "22:00", "activity": "阅读"}]}}) + first = host._agenda_snapshot_window(date_key="2026-07-30", window="late_night") + second = host._agenda_snapshot_window(date_key="2026-07-30", window="late_night") + assert first["snapshot_id"] == second["snapshot_id"] == "agenda_snapshot:2026-07-30:late_night" + assert len(host.data["window_snapshots"]) == 1 + assert len(host.data["agenda_reconciliation_history"]) == 1 + assert "C3日程" in format_agenda_context(host._agenda_build()) + + +def test_closed_windows_use_window_date_and_maintenance_is_local_only() -> None: + host = Host({}) + host.fixed_now = dt("2026-07-30T06:00:00+08:00") + closed = host._agenda_closed_windows(host.fixed_now) + assert ("2026-07-29", "late_night") in closed + settled = host._agenda_maintenance_tick() + assert any(item["window_date"] == "2026-07-29" for item in settled) + assert "world_gateway" not in host.data + + +def test_migrate_store_is_additive_for_old_json() -> None: + old = {"users": {"u": {"name": "keep"}}, "daily_plan": {"date": "2026-07-30", "items": [{"time": "10:00", "activity": "旧计划"}]}} + migrated, changed = migrate_store(old) + assert changed is True + assert migrated["users"] == {"u": {"name": "keep"}} + assert migrated["daily_plan"]["items"][0]["source_kind"] == "planned" + assert migrated["agenda_version"] == 1 + json.dumps(migrated, ensure_ascii=False) diff --git a/tests/test_c3_dual_plugin_link.py b/tests/test_c3_dual_plugin_link.py new file mode 100644 index 0000000..3fc53b3 --- /dev/null +++ b/tests/test_c3_dual_plugin_link.py @@ -0,0 +1,83 @@ +from __future__ import annotations + +import asyncio +import importlib +from pathlib import Path +import sys +import tempfile +import types + + +COMPANION_ROOT = Path(__file__).resolve().parents[1] +MEMORY_ROOT = COMPANION_ROOT.parents[1] / "astrbot_plugin_memory_companion-main" + + +def _load_companion_package(): + name = "c3_dual_companion" + package = types.ModuleType(name) + package.__path__ = [str(COMPANION_ROOT)] + sys.modules[name] = package + return importlib.import_module(f"{name}.bot_personal_contract"), importlib.import_module(f"{name}.bot_personal_outbox") + + +def _load_memory_package(): + name = "c3_dual_memory" + package = types.ModuleType(name) + package.__path__ = [str(MEMORY_ROOT)] + sys.modules[name] = package + core = types.ModuleType(f"{name}.core") + core.__path__ = [str(MEMORY_ROOT / "core")] + sys.modules[core.__name__] = core + bridge = importlib.import_module(f"{name}.core.bridge") + service = importlib.import_module(f"{name}.core.service") + store = importlib.import_module(f"{name}.core.store") + contract = importlib.import_module(f"{name}.core.bot_personal_contract") + return bridge, service, store, contract + + +def test_chat_outbox_delivers_structured_archive_to_memory_bridge_without_domain_leak(): + companion_contract, outbox_module = _load_companion_package() + bridge_module, service_module, store_module, memory_contract = _load_memory_package() + assert companion_contract.CONTRACT_FINGERPRINT == memory_contract.CONTRACT_FINGERPRINT + + with tempfile.TemporaryDirectory() as temporary: + service = object.__new__(service_module.MemoryCompanionService) + service.store = store_module.MemoryStore(Path(temporary) / "memory.db") + service.store.initialize() + service._schedule_memory_embedding = lambda *args, **kwargs: None + bridge = bridge_module.MemoryCompanionBridge(service) + outbox = outbox_module.BotPersonalOutbox({}) + + async def run(): + payload = { + "date": "2026-07-30", + "window": "evening", + "summary": "C3 dual plugin archive", + "items": ["local first", "bridge second"], + } + first = await outbox.enqueue( + memory_type="bot_schedule_plan", + payload=payload, + idempotency_key="daily_plan:2026-07-30", + occurred_at="2026-07-30T19:00:00+08:00", + sender=bridge.record_bot_personal_archive, + ) + duplicate = await outbox.enqueue( + memory_type="bot_schedule_plan", + payload=payload, + idempotency_key="daily_plan:2026-07-30", + occurred_at="2026-07-30T19:00:00+08:00", + sender=bridge.record_bot_personal_archive, + ) + profile = await service.read_bot_personal_profile(limit=10) + return first, duplicate, profile + + try: + first, duplicate, profile = asyncio.run(run()) + assert first["ok"] and first["state"] == "sent" + assert duplicate["deduplicated"] is True + assert profile["read_only"] is True + assert len(profile["items"]) == 1 + assert all("payload" not in item and "content" not in item for item in profile["items"]) + finally: + service.store.close() diff --git a/tests/test_c3_outbox.py b/tests/test_c3_outbox.py new file mode 100644 index 0000000..bd40759 --- /dev/null +++ b/tests/test_c3_outbox.py @@ -0,0 +1,116 @@ +from __future__ import annotations + +import asyncio +from pathlib import Path +import sys +import types + + +ROOT = Path(__file__).resolve().parents[1] +PACKAGE = "c3_outbox_companion" +if PACKAGE not in sys.modules: + package = types.ModuleType(PACKAGE) + package.__path__ = [str(ROOT)] + sys.modules[PACKAGE] = package + +from c3_outbox_companion.bot_personal_outbox import BotPersonalOutbox + + +def run(coro): + return asyncio.run(coro) + + +def payload(): + return {"date": "2026-07-30", "window": "evening", "summary": "整理项目", "items": ["测试"]} + + +def test_enqueue_is_local_first_and_idempotent(): + data = {} + saved = [] + clock = [100.0] + outbox = BotPersonalOutbox(data, save=lambda: saved.append(True), clock=lambda: clock[0]) + + first = run(outbox.enqueue( + memory_type="bot_schedule_plan", + payload=payload(), + idempotency_key="daily_plan:2026-07-30", + occurred_at="2026-07-30T19:00:00+08:00", + )) + duplicate = run(outbox.enqueue( + memory_type="bot_schedule_plan", + payload=payload(), + idempotency_key="daily_plan:2026-07-30", + occurred_at="2026-07-30T19:00:00+08:00", + )) + + assert first["state"] == "pending" + assert duplicate["deduplicated"] is True + assert len(data["bot_personal_outbox"]) == 1 + assert saved + + +def test_drain_success_does_not_duplicate_remote_write(): + data = {} + outbox = BotPersonalOutbox(data, clock=lambda: 100.0) + calls = [] + + async def sender(envelope): + calls.append(envelope["idempotency_key"]) + return {"ok": True, "record_id": "remote-1", "version": 1, "state": "sent"} + + run(outbox.enqueue( + memory_type="bot_window_snapshot", + payload=payload(), + idempotency_key="agenda_snapshot:2026-07-30:evening", + occurred_at="2026-07-30T19:00:00+08:00", + )) + result = run(outbox.drain(sender, now=100.0)) + again = run(outbox.drain(sender, now=100.0)) + + assert result[0]["state"] == "sent" + assert again == [] + assert calls == ["agenda_snapshot:2026-07-30:evening"] + assert outbox.status()["sent"] == 1 + + +def test_retry_backoff_and_dead_letter_are_deterministic(): + data = {} + outbox = BotPersonalOutbox(data, clock=lambda: 100.0, max_attempts=2, base_backoff_seconds=10.0) + + async def sender(_envelope): + return {"ok": False, "state": "degraded", "error_code": "bridge_timeout"} + + run(outbox.enqueue( + memory_type="bot_daily_diary", + payload={"date": "2026-07-30", "summary": "今天完成了回归"}, + idempotency_key="diary:2026-07-30", + occurred_at="2026-07-30T23:00:00+08:00", + )) + first = run(outbox.drain(sender, now=100.0)) + not_due = run(outbox.drain(sender, now=109.9)) + last = run(outbox.drain(sender, now=110.0)) + + assert first[0]["state"] == "retry" + assert not_due == [] + assert last[0]["state"] == "dead_letter" + assert outbox.status()["dead_letter"] == 1 + + +def test_newer_version_replaces_pending_and_same_version_conflict_is_rejected(): + data = {} + outbox = BotPersonalOutbox(data, clock=lambda: 100.0) + kwargs = { + "memory_type": "bot_schedule_reconciliation", + "idempotency_key": "reconciliation:2026-07-30:evening", + "occurred_at": "2026-07-30T22:00:00+08:00", + } + first = run(outbox.enqueue(payload={**payload(), "summary": "old"}, version=1, **kwargs)) + conflict = run(outbox.enqueue(payload={**payload(), "summary": "conflict"}, version=1, **kwargs)) + newer = run(outbox.enqueue(payload={**payload(), "summary": "new"}, version=2, **kwargs)) + stale = run(outbox.enqueue(payload={**payload(), "summary": "stale"}, version=1, **kwargs)) + + assert first["state"] == "pending" + assert conflict["state"] == "version_conflict" + assert newer["state"] == "pending" + assert stale["state"] == "stale_version" + assert data["bot_personal_outbox"][0]["version"] == 2 diff --git a/unified_agenda.py b/unified_agenda.py new file mode 100644 index 0000000..d81a409 --- /dev/null +++ b/unified_agenda.py @@ -0,0 +1,111 @@ +# -*- coding: utf-8 -*- +"""Read model and prompt formatter for the local C3 agenda.""" +from __future__ import annotations + +from datetime import datetime +from typing import Any + +try: + from .agenda_contracts import ( + SCHEDULE_WINDOWS, + interval_overlaps_window, + parse_datetime, + window_bounds, + window_for_datetime, + ) + from .schedule_reconciler import reconcile +except ImportError: + from agenda_contracts import SCHEDULE_WINDOWS, interval_overlaps_window, parse_datetime, window_bounds, window_for_datetime + from schedule_reconciler import reconcile + + +def build_unified_agenda( + *, + plans: list[dict[str, Any]], + activities: list[dict[str, Any]], + now: datetime, + date_key: str = "", + timezone_name: str = "Asia/Shanghai", +) -> dict[str, Any]: + result = reconcile(plans or [], activities or [], now=now) + current_slug, current_window_date, _current_start, _current_end = window_for_datetime(now, timezone_name=timezone_name) + target_date = date_key or current_window_date + entries = [ + item for item in result["entries"] + if not target_date or str(item.get("date") or item.get("start_at") or "")[:10] == target_date + ] + current = None + for item in sorted(result["entries"], key=lambda value: str(value.get("start_at") or "")): + if item.get("status") in {"cancelled", "unknown"}: + continue + try: + start_text = item.get("start_at") or item.get("start") + end_text = item.get("end_at") or item.get("end") + item_start = parse_datetime(start_text, timezone_name=timezone_name) + item_end = parse_datetime(end_text, timezone_name=timezone_name, default=item_start) + if item_start <= now.astimezone(item_start.tzinfo) < item_end: + current = item + break + except Exception: + continue + + windows: list[dict[str, Any]] = [] + for slug, _name, _start_minute, _end_minute in SCHEDULE_WINDOWS: + start, end = window_bounds(target_date, slug, timezone_name=timezone_name) + window_plans = [item for item in result["plans"] if interval_overlaps_window(item, start, end, timezone_name=timezone_name)] + window_activities = [item for item in result["activities"] if interval_overlaps_window(item, start, end, timezone_name=timezone_name)] + window_reconciliations = [ + item for item in result["reconciliations"] + if any(str(plan.get("plan_id")) in {str(p.get("plan_id")) for p in window_plans} for plan in [item]) + ] + windows.append( + { + "slug": slug, + "window": slug, + "window_date": target_date, + "start_at": start.isoformat(timespec="seconds"), + "end_at": end.isoformat(timespec="seconds"), + "planned": window_plans, + "observed": window_activities, + "reconciled": window_reconciliations, + } + ) + + return { + "generated_at": now.isoformat(timespec="seconds"), + "date": target_date, + "window_date": target_date, + "current_window": current_slug if target_date == current_window_date else "", + "current": current, + "entries": entries, + "plans": result["plans"], + "activities": result["activities"], + "matched": result["matched"], + "reconciliations": result["reconciliations"], + "windows": windows, + } + + +def format_agenda_context(agenda: dict[str, Any], *, max_entries: int = 8) -> str: + if not isinstance(agenda, dict): + return "" + lines = [f"C3日程({agenda.get('window_date') or agenda.get('date') or '未知日期'})"] + current = agenda.get("current") + if isinstance(current, dict): + lines.append( + f"当前实际:{str(current.get('title') or '未命名')[:80]} " + f"[{current.get('evidence_level') or 'L?'}|{current.get('status') or 'unknown'}]" + ) + entries = agenda.get("entries") if isinstance(agenda.get("entries"), list) else [] + for item in entries[: max(0, int(max_entries))]: + if not isinstance(item, dict): + continue + kind = "实际" if item.get("kind") == "observed" else "计划" + title = str(item.get("title") or "未命名")[:80] + status = str(item.get("status") or "unknown") + reason = str(item.get("reconciliation_reason") or "") + suffix = f";{reason}" if reason else "" + lines.append(f"- {kind}:{title} [{status}]{suffix}") + if len(lines) == 1: + lines.append("- 暂无记录") + return "\n".join(lines)