M6.1.3-R2 非五类兼容(阻断修复):DTO date 缺失时从 occurred_at 派生, 刻意不做时区换算以与 _infer_window 同一时钟;calendar_event 取 start_at、 creative_progress 取 project.updated_at;未知 kind 不再默认 bot_observed_activity; capture_hard_fact L4→L3;TYPE_CONTRACTS 覆盖全部 12 类,修复非五类 source_kind 兜底成 observed 污染本地记录;proactive_message 的 image_path 改为 has_image+ image_name(原样透传会被自身隐私校验整条静默丢弃);snapshot/daily_plan 透传 version。 M6.1.4 创作/媒体/主观分域:creative_progress 补 chunk 形参(生产调用点一直 TypeError 导致创作推进整步被阻断)并只留里程碑摘要;photo_generation 改为媒体索引 (原 payload 带 prompt 键与绝对路径,100% 静默丢弃);幂等键纳入隐私校验; 禁用键清单收窄,放行媒体标识键;两条 Bridge 路径统一送 body payload。 M6.1.5 Bot 专用检索 Profiles:按 Profile 分槽注入,固定槽位与条数、跨槽去重、 声明当前以本地运行态为准;旧版记忆插件回退通用检索。 M6.1.6 分层只读 API:新增 build_agenda_runtime_view 与 GET /agenda/runtime, 记忆插件停用时仍完整返回并标降级。 M6.1.7 隐私与残余收口:DTO 新增 contexts 禁用键;已删除娱乐功能(qzone/news/ web_exploration)的残余配置入口从展示面隐藏,旧值保留不删。 测试:174 项通过(pytest 与 unittest 双口径)。联合 E2E 135 项全部通过。 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
368 行
无行尾
19 KiB
Python
368 行
无行尾
19 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Runtime integration for the local unified agenda."""
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
from copy import deepcopy
|
|
from datetime import datetime, timedelta
|
|
from typing import Any
|
|
|
|
try:
|
|
from .activity_capture import ActivityCapture
|
|
from .agenda_contracts import interval_overlaps_window, migrate_store, normalize_observed_activity, normalize_window_snapshot, window_bounds, window_for_datetime
|
|
from .unified_agenda import build_unified_agenda, format_agenda_context
|
|
from .world_gateway import NullWorldGateway, WorldGatewayContract
|
|
from .calendar_system import plan_item_to_event
|
|
except ImportError:
|
|
from activity_capture import ActivityCapture
|
|
from agenda_contracts import interval_overlaps_window, migrate_store, normalize_observed_activity, normalize_window_snapshot, window_bounds, window_for_datetime
|
|
from unified_agenda import build_unified_agenda, format_agenda_context
|
|
from world_gateway import NullWorldGateway, WorldGatewayContract
|
|
from calendar_system import plan_item_to_event
|
|
|
|
|
|
class AgendaRuntimeMixin:
|
|
def _agenda_prepare_store(self) -> None:
|
|
data, changed = migrate_store(self.data)
|
|
self.data = data
|
|
if not hasattr(self, "_agenda_capture"):
|
|
self._agenda_capture = ActivityCapture()
|
|
if not hasattr(self, "_world_gateway"):
|
|
self._world_gateway = WorldGatewayContract(NullWorldGateway())
|
|
if changed and hasattr(self, "_schedule_data_save"):
|
|
self._schedule_data_save(delay=0.0)
|
|
|
|
def _agenda_activities_store(self) -> list[dict[str, Any]]:
|
|
self._agenda_prepare_store()
|
|
activities = self.data.setdefault("observed_activities", [])
|
|
if not isinstance(activities, list):
|
|
activities = []
|
|
self.data["observed_activities"] = activities
|
|
return activities
|
|
|
|
def _agenda_snapshots_store(self) -> list[dict[str, Any]]:
|
|
self._agenda_prepare_store()
|
|
snapshots = self.data.setdefault("window_snapshots", [])
|
|
if not isinstance(snapshots, list):
|
|
snapshots = []
|
|
self.data["window_snapshots"] = snapshots
|
|
return snapshots
|
|
|
|
def _agenda_schedule_observed_archive(self, activity: dict[str, Any]) -> None:
|
|
recorder = getattr(self, "_memory_companion_record_observed_activity", None)
|
|
if not callable(recorder) or not isinstance(activity, dict):
|
|
return
|
|
try:
|
|
loop = asyncio.get_running_loop()
|
|
except RuntimeError:
|
|
return
|
|
|
|
async def deliver() -> None:
|
|
try:
|
|
await recorder(deepcopy(activity))
|
|
except Exception:
|
|
pass
|
|
|
|
loop.create_task(deliver())
|
|
|
|
def _agenda_capture_inbound_message(
|
|
self,
|
|
*,
|
|
text: str,
|
|
event_time: datetime,
|
|
source_ref: str,
|
|
conversation_id: str,
|
|
participant: str = "user",
|
|
message_count: int = 1,
|
|
) -> 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,
|
|
)
|
|
if candidate is None:
|
|
return None
|
|
activities = self._agenda_activities_store()
|
|
source_ref = str(candidate.get("source_refs", [""])[0] or "") if isinstance(candidate.get("source_refs"), list) else ""
|
|
duplicate = next((item for item in activities if source_ref and source_ref in (item.get("source_refs") or [])), None)
|
|
existing = duplicate or next((item for item in activities if isinstance(item, dict) and item.get("activity_id") == candidate.get("activity_id")), None)
|
|
if duplicate is not None:
|
|
final_activity = deepcopy(existing)
|
|
elif existing is not None:
|
|
prior_refs = list(existing.get("source_refs") or [])
|
|
prior_refs.extend(candidate.get("source_refs") or [])
|
|
candidate["source_refs"] = list(dict.fromkeys(str(item) for item in prior_refs if str(item)))[:30]
|
|
candidate["start_at"] = min(str(existing.get("start_at") or candidate.get("start_at")), str(candidate.get("start_at") or existing.get("start_at")))
|
|
candidate["end_at"] = max(str(existing.get("end_at") or candidate.get("end_at")), str(candidate.get("end_at") or existing.get("end_at")))
|
|
existing.update(candidate)
|
|
existing["version"] = int(existing.get("version") or 1) + 1
|
|
final_activity = deepcopy(existing)
|
|
else:
|
|
candidate["version"] = max(1, int(candidate.get("version") or 1))
|
|
activities.append(candidate)
|
|
final_activity = deepcopy(candidate)
|
|
activities[:] = activities[-500:]
|
|
if hasattr(self, "_schedule_data_save"):
|
|
self._schedule_data_save()
|
|
if duplicate is None:
|
|
self._agenda_schedule_observed_archive(final_activity)
|
|
return final_activity
|
|
|
|
def _agenda_capture_hard_fact(self, activity: dict[str, Any]) -> dict[str, Any]:
|
|
self._agenda_prepare_store()
|
|
normalized = normalize_observed_activity(activity)
|
|
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:
|
|
normalized["version"] = max(1, int(normalized.get("version") or 1))
|
|
activities.append(normalized)
|
|
final_activity = deepcopy(normalized)
|
|
else:
|
|
existing.update(normalized)
|
|
existing["version"] = int(existing.get("version") or 1) + 1
|
|
final_activity = deepcopy(existing)
|
|
activities[:] = activities[-500:]
|
|
if hasattr(self, "_schedule_data_save"):
|
|
self._schedule_data_save()
|
|
self._agenda_schedule_observed_archive(final_activity)
|
|
return final_activity
|
|
|
|
def _agenda_current_plan_items(self) -> list[dict[str, Any]]:
|
|
events = self.data.get("calendar_events") if isinstance(self.data.get("calendar_events"), list) else []
|
|
projected = []
|
|
for event in events:
|
|
if not isinstance(event, dict):
|
|
continue
|
|
source = str(event.get("source") or "")
|
|
source_kind = str(event.get("source_kind") or "")
|
|
if source == "daily_plan_projection" or source_kind == "planned":
|
|
item = dict(event)
|
|
item["plan_id"] = str(item.get("plan_id") or item.get("event_id") or "")
|
|
item["title"] = str(item.get("title") or item.get("description") or "")
|
|
projected.append(item)
|
|
if projected:
|
|
return projected
|
|
plan = self.data.get("daily_plan") if isinstance(self.data.get("daily_plan"), dict) else {}
|
|
items = plan.get("items") if isinstance(plan.get("items"), list) else []
|
|
result = []
|
|
plan_date = str(plan.get("date") or "")
|
|
now = self._calendar_now() if hasattr(self, "_calendar_now") else datetime.now().astimezone()
|
|
for index, item in enumerate(items):
|
|
if not isinstance(item, dict):
|
|
continue
|
|
try:
|
|
event = plan_item_to_event(plan_date, item, timezone_name=getattr(self, "_calendar_timezone_name", lambda: "Asia/Shanghai")(), index=index, now=now)
|
|
except Exception:
|
|
continue
|
|
event["plan_id"] = event.get("event_id")
|
|
event["title"] = event.get("title") or event.get("description")
|
|
result.append(event)
|
|
return result
|
|
|
|
def _agenda_build(self, *, date_key: str = "") -> dict[str, Any]:
|
|
self._agenda_prepare_store()
|
|
now = self._calendar_now() if hasattr(self, "_calendar_now") else datetime.now().astimezone()
|
|
return build_unified_agenda(
|
|
plans=self._agenda_current_plan_items(),
|
|
activities=self._agenda_activities_store(),
|
|
now=now,
|
|
date_key=date_key,
|
|
)
|
|
|
|
def _agenda_context_for_prompt(self, *, max_entries: int = 8) -> str:
|
|
return format_agenda_context(self._agenda_build(), max_entries=max_entries)
|
|
|
|
def _agenda_reconciliation_store(self) -> list[dict[str, Any]]:
|
|
self._agenda_prepare_store()
|
|
history = self.data.setdefault("agenda_reconciliation_history", [])
|
|
if not isinstance(history, list):
|
|
history = []
|
|
self.data["agenda_reconciliation_history"] = history
|
|
return history
|
|
|
|
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_window_items(self, *, date_key: str, window: str) -> tuple[list[dict[str, Any]], list[dict[str, Any]], datetime, datetime]:
|
|
start, end = window_bounds(date_key, window, timezone_name=self._agenda_timezone_name())
|
|
plans = [item for item in self._agenda_current_plan_items() if interval_overlaps_window(item, start, end, timezone_name=self._agenda_timezone_name())]
|
|
activities = [item for item in self._agenda_activities_store() if interval_overlaps_window(item, start, end, timezone_name=self._agenda_timezone_name())]
|
|
return plans, activities, start, end
|
|
|
|
def _agenda_snapshot_window(self, *, date_key: str, window: str, open_items: list[str] | None = None) -> dict[str, Any]:
|
|
plans, activities, start, end = self._agenda_window_items(date_key=date_key, window=window)
|
|
now = self._calendar_now() if hasattr(self, "_calendar_now") else datetime.now().astimezone()
|
|
agenda = build_unified_agenda(plans=plans, activities=activities, now=now, date_key=date_key)
|
|
reconciliation = [
|
|
{
|
|
"plan_id": item.get("plan_id") or item.get("event_id"),
|
|
"status": item.get("status") or "unknown",
|
|
"reason": item.get("reconciliation_reason") or "",
|
|
"activity_ids": list(item.get("reconciled_activity_ids") or []),
|
|
}
|
|
for item in agenda.get("plans", []) if isinstance(item, dict)
|
|
]
|
|
snapshot = normalize_window_snapshot(
|
|
{
|
|
"snapshot_id": self._agenda_snapshot_id(date_key, window),
|
|
"date": date_key,
|
|
"window_date": date_key,
|
|
"window": window,
|
|
"start_at": start.isoformat(timespec="seconds"),
|
|
"end_at": end.isoformat(timespec="seconds"),
|
|
"timezone": self._agenda_timezone_name(),
|
|
"planned": [item.get("title") or item.get("activity") for item in plans if isinstance(item, dict)],
|
|
"observed": [item.get("title") for item in activities if isinstance(item, dict)],
|
|
"reconciled": reconciliation,
|
|
"open_items": list(open_items or []),
|
|
"source_refs": [item.get("activity_id") for item in activities if item.get("activity_id")],
|
|
"source_kind": "projection",
|
|
"evidence_level": "L2",
|
|
"certainty": "medium",
|
|
},
|
|
now=now,
|
|
)
|
|
snapshots = self._agenda_snapshots_store()
|
|
existing = next((item for item in snapshots if item.get("snapshot_id") == snapshot.get("snapshot_id")), None)
|
|
if existing is None:
|
|
snapshots.append(snapshot)
|
|
else:
|
|
created_at = existing.get("created_at") or existing.get("generated_at")
|
|
snapshot["version"] = int(existing.get("version") or 1) + 1
|
|
if created_at:
|
|
snapshot["created_at"] = created_at
|
|
existing.clear()
|
|
existing.update(snapshot)
|
|
snapshots[:] = snapshots[-120:]
|
|
history = self._agenda_reconciliation_store()
|
|
reconciliation_id = f"reconciliation:{date_key}:{window}"
|
|
record = {
|
|
"reconciliation_id": reconciliation_id,
|
|
"date": date_key,
|
|
"window_date": date_key,
|
|
"window": window,
|
|
"start_at": snapshot.get("start_at"),
|
|
"end_at": snapshot.get("end_at"),
|
|
"timezone": self._agenda_timezone_name(),
|
|
"plans": reconciliation,
|
|
"observed_activity_ids": list(snapshot.get("source_refs") or []),
|
|
"source_refs": [snapshot.get("snapshot_id")],
|
|
"source_kind": "real",
|
|
"evidence_level": "L3",
|
|
"certainty": "high" if reconciliation else "medium",
|
|
"status": "completed",
|
|
"generated_at": snapshot.get("generated_at"),
|
|
"version": 1,
|
|
}
|
|
existing_record = next((item for item in history if item.get("reconciliation_id") == reconciliation_id), None)
|
|
if existing_record is None:
|
|
history.append(record)
|
|
else:
|
|
record["version"] = int(existing_record.get("version") or 1) + 1
|
|
existing_record.clear()
|
|
existing_record.update(record)
|
|
history[:] = history[-240:]
|
|
if hasattr(self, "_schedule_data_save"):
|
|
self._schedule_data_save()
|
|
return snapshot
|
|
|
|
def _agenda_window_for_now(self, now: datetime | None = None) -> tuple[str, int, int]:
|
|
current = now or (self._calendar_now() if hasattr(self, "_calendar_now") else datetime.now().astimezone())
|
|
name, _, start, end = window_for_datetime(current, timezone_name=self._agenda_timezone_name())
|
|
return name, start.hour * 60 + start.minute, end.hour * 60 + end.minute
|
|
|
|
def _agenda_snapshot_id(self, date_key: str, window: str) -> str:
|
|
return f"snapshot-{date_key}-{window}"
|
|
|
|
def _agenda_closed_windows(self, now: datetime) -> list[tuple[str, str]]:
|
|
timezone_name = self._agenda_timezone_name()
|
|
local_now = now
|
|
if local_now.tzinfo is None:
|
|
local_now = local_now.replace(tzinfo=window_bounds(now.date(), "早晨", timezone_name=timezone_name)[0].tzinfo)
|
|
local_now = local_now.astimezone(window_bounds(local_now.date(), "早晨", timezone_name=timezone_name)[0].tzinfo)
|
|
existing_ids = {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.date() + timedelta(days=offset)
|
|
for name, _, _ in (("深夜", 0, 0), ("早晨", 0, 0), ("下午", 0, 0), ("晚上", 0, 0)):
|
|
_, end = window_bounds(target, name, timezone_name=timezone_name)
|
|
snapshot_id = self._agenda_snapshot_id(target.isoformat(), name)
|
|
if end <= local_now and snapshot_id not in existing_ids:
|
|
candidates.append((target.isoformat(), name))
|
|
return candidates
|
|
|
|
async def _agenda_maintenance_tick(self) -> None:
|
|
"""Settle closed local windows, recover missed windows after restart, and flush optional archive delivery."""
|
|
self._agenda_prepare_store()
|
|
now = self._calendar_now() if hasattr(self, "_calendar_now") else datetime.now().astimezone()
|
|
for date_key, window in self._agenda_closed_windows(now):
|
|
snapshot = self._agenda_snapshot_window(date_key=date_key, window=window)
|
|
recorder = getattr(self, "_memory_companion_record_agenda_snapshot", None)
|
|
if callable(recorder):
|
|
try:
|
|
await recorder(snapshot)
|
|
except Exception:
|
|
pass
|
|
history = self._agenda_reconciliation_store()
|
|
reconciliation = next((item for item in reversed(history) if item.get("reconciliation_id") == f"reconciliation:{date_key}:{window}"), None)
|
|
reconciliation_recorder = getattr(self, "_memory_companion_record_reconciliation", None)
|
|
if callable(reconciliation_recorder) and isinstance(reconciliation, dict):
|
|
try:
|
|
await reconciliation_recorder(reconciliation)
|
|
except Exception:
|
|
pass
|
|
sleep_text = str(getattr(self, "daily_diary_time", "23:10") or "23:10")
|
|
try:
|
|
hour_text, minute_text = sleep_text.split(":", 1)
|
|
bedtime_minute = int(hour_text) * 60 + int(minute_text)
|
|
except (TypeError, ValueError):
|
|
bedtime_minute = 1390
|
|
lead = max(0, int(getattr(self, "sleep_summary_lead_minutes", 30) or 30))
|
|
if now.hour * 60 + now.minute >= max(0, bedtime_minute - lead):
|
|
diary = self.data.get("bot_diaries", [])
|
|
date_key = now.date().isoformat()
|
|
today_has_diary = isinstance(diary, list) and any(isinstance(item, dict) and str(item.get("date") or "") == date_key for item in diary)
|
|
if not today_has_diary and bool(getattr(self, "enable_daily_diary", True)):
|
|
ensure_diary = getattr(self, "_ensure_daily_diary", None)
|
|
if callable(ensure_diary):
|
|
try:
|
|
await ensure_diary()
|
|
except Exception:
|
|
pass
|
|
flusher = getattr(self, "_memory_companion_flush_bot_personal_outbox", None)
|
|
if callable(flusher):
|
|
try:
|
|
await flusher(limit=10)
|
|
except Exception:
|
|
pass
|
|
def _world_get_context(self, subject_id: str) -> dict[str, Any]:
|
|
"""Return only cached World context; production networking is disabled in Q5."""
|
|
self._agenda_prepare_store()
|
|
now = self._calendar_now() if hasattr(self, "_calendar_now") else datetime.now().astimezone()
|
|
cache = self.data.get("world_projection_cache") if isinstance(self.data.get("world_projection_cache"), list) else []
|
|
for item in reversed(cache):
|
|
if not isinstance(item, dict) or str(item.get("subject_id") or "") != str(subject_id):
|
|
continue
|
|
fresh_until = str(item.get("fresh_until") or "")
|
|
try:
|
|
expiry = datetime.fromisoformat(fresh_until.replace("Z", "+00:00"))
|
|
if expiry.tzinfo is None:
|
|
expiry = expiry.replace(tzinfo=now.tzinfo)
|
|
if now <= expiry:
|
|
return {"status": "cached", "fresh": True, "projection": item}
|
|
except (TypeError, ValueError):
|
|
continue
|
|
return {"status": "offline", "fresh": False, "projection": None}
|
|
|
|
def _world_publish_activity(self, activity: dict[str, Any]) -> dict[str, Any]:
|
|
"""Keep the future publish boundary explicit without sending to a server."""
|
|
if not bool(getattr(self, "enable_world_gateway", False)):
|
|
return {"ok": False, "reason": "world_gateway_disabled"}
|
|
result = self._world_gateway.publish_activity(activity)
|
|
return {"ok": bool(result.ok), "reason": result.reason, "data": result.data} |