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>
131 行
5.3 KiB
Python
131 行
5.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Deterministic plan/actual reconciliation for the local agenda."""
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
from typing import Any
|
|
import re
|
|
|
|
try:
|
|
from .agenda_contracts import agenda_entry_from_activity, agenda_entry_from_plan, normalize_observed_activity
|
|
from .calendar_system import parse_datetime
|
|
except ImportError:
|
|
from agenda_contracts import agenda_entry_from_activity, agenda_entry_from_plan, normalize_observed_activity
|
|
from calendar_system import parse_datetime
|
|
|
|
PRIORITY = {"L0": 0, "L1": 1, "L2": 2, "L3": 3, "L4": 4, "L5": 5}
|
|
|
|
|
|
def _normalized_text(value: Any) -> str:
|
|
text = str(value or "").lower().strip()
|
|
text = re.sub(r"[^0-9a-z\u4e00-\u9fff]+", "", text)
|
|
return text
|
|
|
|
|
|
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"), default=now)
|
|
except Exception:
|
|
return None, None
|
|
try:
|
|
end = parse_datetime(item.get("end_at"), default=start)
|
|
except Exception:
|
|
end = start
|
|
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)
|
|
if not ps or not a_s:
|
|
return False
|
|
if pe <= ps:
|
|
pe = ps
|
|
if a_e <= a_s:
|
|
a_e = a_s
|
|
return ps < a_e and a_s < pe
|
|
|
|
|
|
def _title_matches(plan: dict[str, Any], activity: dict[str, Any]) -> bool:
|
|
plan_id = str(plan.get("plan_id") or plan.get("event_id") or "")
|
|
refs = {str(item) for item in (activity.get("source_refs") or []) if str(item)}
|
|
if plan_id and plan_id in refs:
|
|
return True
|
|
plan_text = _normalized_text(plan.get("title") or plan.get("activity"))
|
|
activity_text = _normalized_text(activity.get("title"))
|
|
if not plan_text or not activity_text:
|
|
return False
|
|
if plan_text in activity_text or activity_text in plan_text:
|
|
return True
|
|
plan_tokens = _tokens(plan_text)
|
|
activity_tokens = _tokens(activity_text)
|
|
shared = plan_tokens.intersection(activity_tokens)
|
|
return bool(shared and (any(len(token) >= 3 for token in shared) or len(shared) >= 2))
|
|
|
|
|
|
def reconcile(
|
|
plans: list[dict[str, Any]],
|
|
activities: list[dict[str, Any]],
|
|
*,
|
|
now: datetime,
|
|
) -> dict[str, Any]:
|
|
normalized_activities = []
|
|
for activity in activities:
|
|
try:
|
|
normalized_activities.append(normalize_observed_activity(activity, now=now))
|
|
except Exception:
|
|
continue
|
|
matched: dict[str, list[dict[str, Any]]] = {}
|
|
used_activity_ids: set[str] = set()
|
|
results = []
|
|
entries = []
|
|
for raw_plan in plans:
|
|
plan = dict(raw_plan)
|
|
plan_id = str(plan.get("plan_id") or plan.get("event_id") or "")
|
|
overlaps = [activity for activity in normalized_activities if _overlaps(plan, activity, now)]
|
|
matching = [activity for activity in overlaps if _title_matches(plan, activity)]
|
|
selected = matching or overlaps
|
|
if selected:
|
|
matched[plan_id] = selected
|
|
used_activity_ids.update(str(item.get("activity_id")) for item in selected)
|
|
strongest = max((PRIORITY.get(str(item.get("evidence_level")), 0) for item in selected), default=0)
|
|
if matching:
|
|
status = "completed" if all(item.get("status") == "completed" for item in selected) else "active"
|
|
reason = "实际活动与计划主题和时间匹配"
|
|
elif strongest >= PRIORITY["L2"]:
|
|
status = "overridden"
|
|
reason = "实际活动覆盖原计划"
|
|
else:
|
|
status = "unknown"
|
|
reason = "活动证据不足"
|
|
plan["status"] = status
|
|
plan["reconciliation_reason"] = reason
|
|
plan["reconciled_activity_ids"] = [item.get("activity_id") for item in selected]
|
|
entries.append(agenda_entry_from_plan(plan, reason=reason))
|
|
entries.extend(agenda_entry_from_activity(item, source_refs=[plan_id, item.get("activity_id")]) for item in selected)
|
|
else:
|
|
if plan.get("status") not in {"cancelled", "deferred"} and _plan_is_past(plan, now):
|
|
plan["status"] = "unknown"
|
|
plan["reconciliation_reason"] = "窗口结束但没有足够实际证据"
|
|
entries.append(agenda_entry_from_plan(plan, reason=plan.get("reconciliation_reason", "")))
|
|
results.append(plan)
|
|
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: (0 if item.get("kind") == "observed_activity" else 1, str(item.get("start_at") or "")))
|
|
return {"plans": results, "activities": normalized_activities, "entries": entries, "matched": matched}
|
|
|
|
|
|
def _plan_is_past(plan: dict[str, Any], now: datetime) -> bool:
|
|
_, end = _interval(plan, now)
|
|
return bool(end and end <= now)
|