feat(c4): add isolated bot profiles and capability states

这个提交存在于:
Codex Local 2026-07-30 04:50:15 +08:00
父节点 5ca70d45bd
当前提交 44b9359d59
共有 9 个文件被更改,包括 1166 次插入 和 6 次删除

查看文件

@ -6,9 +6,11 @@ from zoneinfo import ZoneInfo
from . import bot_personal_contract
from .bot_personal_dto import BotPersonalArchiveDTO, build_bot_personal_archive
from .capability_probe import CapabilityCache, PROFILE_NAMES as C4_PROFILE_NAMES, build_capability_snapshot
from .context_consumer import consume_context_projection
from .models import EntityRef, MemoryRecord, SessionContext, clean_text
from .person_projection import consume_person_projection
from .profile_api import build_profile_result
LOCAL_TZ = ZoneInfo("Asia/Shanghai")
@ -37,6 +39,7 @@ class MemoryCompanionBridge:
def __init__(self, plugin: Any):
self._plugin = plugin
self._capability_cache = CapabilityCache()
async def record_event(
self,
@ -315,6 +318,75 @@ class MemoryCompanionBridge:
async def search_bot_personal_profile(self, query: str = "", *, limit: int = 10) -> dict[str, Any]:
return await self.read_bot_personal_profile(query=query, limit=limit)
async def read_bot_profile(
self,
profile: str,
query: str = "",
*,
limit: int = 10,
current_date: str = "",
current_window: str = "",
authorized: bool = False,
) -> dict[str, Any]:
"""Read one C4 Bot Profile through a privacy-limited bridge boundary."""
base = {
"ok": False,
"read_only": True,
"state": "degraded",
"degraded": True,
"pending": True,
"profile": clean_text(profile, 80),
"items": [],
"warnings": [],
}
try:
getter = getattr(self._plugin, "read_bot_profile", None)
except Exception:
getter = None
if not callable(getter):
return {**base, "error_code": "bridge_method_unavailable"}
try:
result = await getter(
profile,
query=query,
limit=limit,
current_date=current_date,
current_window=current_window,
authorized=authorized,
)
except Exception:
return {**base, "error_code": "bridge_exception"}
if not isinstance(result, dict):
return {**base, "error_code": "invalid_bridge_response"}
safe_item_keys = {
"record_id", "memory_domain", "memory_type", "subject", "date", "window",
"occurred_at", "source_kind", "source_refs", "evidence_level", "status",
"version", "summary", "reference",
}
safe_items: list[dict[str, Any]] = []
items = result.get("items", [])
for item in items if isinstance(items, list) else []:
if not isinstance(item, dict):
continue
safe_items.append({key: item[key] for key in safe_item_keys if key in item})
return {
"ok": bool(result.get("ok", True)),
"read_only": True,
"state": clean_text(result.get("state"), 40) or "ready",
"degraded": bool(result.get("degraded", False)),
"pending": bool(result.get("pending", False)),
"profile": clean_text(result.get("profile") or profile, 80),
"items": safe_items,
"warnings": [clean_text(item, 160) for item in result.get("warnings", []) if clean_text(item, 160)][:8]
if isinstance(result.get("warnings"), list) else [],
}
async def read_profile(self, profile: str, query: str = "", **kwargs: Any) -> dict[str, Any]:
"""Short alias for callers that use the generic Profile API name."""
return await self.read_bot_profile(profile, query=query, **kwargs)
async def search(
self,
query: str,
@ -430,13 +502,15 @@ class MemoryCompanionBridge:
companion_available=companion_available,
)
def probe_bot_personal_memory_capabilities(self) -> dict[str, Any]:
"""Return a database-free capability snapshot for the bot-personal contract.
def probe_capability_snapshot(self) -> dict[str, Any]:
"""Return the C4 capability snapshot without touching plugin state or storage.
The probe is intentionally based only on the shared contract module. It
must remain safe to call from ordinary chat paths even when the contract
is stale or the local module is otherwise malformed.
"""
if self._capability_cache.snapshot().get("state") == "negative":
return self.capability_status()
try:
descriptor = bot_personal_contract.capability_descriptor(
available=True,
@ -482,24 +556,75 @@ class MemoryCompanionBridge:
)
result["available"] = True
result["state"] = "ready"
result["state"] = "available"
result["degraded"] = False
self._add_personal_capability_contract_aliases(result)
c4_snapshot = build_capability_snapshot(
available=True,
state="available",
contract_module=bot_personal_contract,
methods=result.get("methods", []),
profiles=C4_PROFILE_NAMES,
warnings=result.get("warnings", []),
)
result.update(c4_snapshot)
result["memory_domain"] = bot_personal_contract.BOT_PERSONAL_MEMORY_DOMAIN
result["domain"] = bot_personal_contract.BOT_PERSONAL_MEMORY_DOMAIN
result["contract_revision"] = bot_personal_contract.CONTRACT_REVISION
result["capability_schema_version"] = bot_personal_contract.BOT_PERSONAL_CAPABILITY_SCHEMA_VERSION
result["payload_schema_version"] = bot_personal_contract.BOT_PERSONAL_PAYLOAD_SCHEMA_VERSION
result["capability_state"] = "available"
self._capability_cache.mark_available(c4_snapshot)
result.setdefault("warnings", [])
return result
def probe_bot_personal_memory_capabilities(self) -> dict[str, Any]:
"""Backward-compatible C1 probe; C4 state is exposed as capability_state."""
result = dict(self.probe_capability_snapshot())
if result.get("capability_state") == "available":
result["state"] = "ready"
result["legacy_state"] = result.get("state", "degraded")
return result
def capability_status(self) -> dict[str, Any]:
"""Return the bounded C4 cache state without probing storage."""
snapshot = self._capability_cache.snapshot()
snapshot["read_only"] = False
snapshot["contract_name"] = bot_personal_contract.CONTRACT_NAME
snapshot["max_payload_bytes"] = bot_personal_contract.BOT_PERSONAL_MAX_PAYLOAD_BYTES
snapshot["memory_domain"] = bot_personal_contract.BOT_PERSONAL_MEMORY_DOMAIN
snapshot["domain"] = snapshot["memory_domain"]
snapshot["contract_revision"] = bot_personal_contract.CONTRACT_REVISION
snapshot["capability_schema_version"] = bot_personal_contract.BOT_PERSONAL_CAPABILITY_SCHEMA_VERSION
snapshot["payload_schema_version"] = bot_personal_contract.BOT_PERSONAL_PAYLOAD_SCHEMA_VERSION
snapshot["capability_state"] = snapshot.get("state", "unprobed")
return snapshot
def mark_capability_negative(self, reason: str) -> dict[str, Any]:
"""Temporarily suppress repeated capability failures at the Bridge edge."""
self._capability_cache.mark_negative(clean_text(reason, 120) or "capability_negative")
return self.capability_status()
@staticmethod
def _add_personal_capability_contract_aliases(result: dict[str, Any]) -> dict[str, Any]:
"""Expose stable C1 aliases without changing the shared contract copy."""
result.setdefault("domain", result.get("memory_domain", ""))
result.setdefault("domains", [result.get("memory_domain", "")])
result.setdefault("profiles", ["bot_personal_archive"])
result.setdefault("profiles", list(C4_PROFILE_NAMES))
result.setdefault("legacy_profiles", ["bot_personal_archive"])
result.setdefault(
"methods",
[
"record_event",
"record_visible_turn",
"record_bot_personal_archive",
"record_bot_personal_memory",
"read_bot_personal_profile",
"search_bot_personal_profile",
"search",
"compose_injection",
"compose_context",
@ -507,6 +632,9 @@ class MemoryCompanionBridge:
"recall",
"consume_person_projection",
"consume_context_projection",
"read_bot_profile",
"read_profile",
"probe_capability_snapshot",
"probe_bot_personal_memory_capabilities",
],
)
@ -548,7 +676,28 @@ class MemoryCompanionBridge:
"warnings": list(warnings or [reason]),
}
)
return MemoryCompanionBridge._add_personal_capability_contract_aliases(result)
MemoryCompanionBridge._add_personal_capability_contract_aliases(result)
c4_snapshot = build_capability_snapshot(
available=False,
state="degraded",
contract_module=bot_personal_contract,
methods=result.get("methods", []),
profiles=C4_PROFILE_NAMES,
warnings=result.get("warnings", []),
error_code=reason,
)
result.update(c4_snapshot)
result["memory_domain"] = getattr(bot_personal_contract, "BOT_PERSONAL_MEMORY_DOMAIN", "")
result["domain"] = result["memory_domain"]
result["contract_revision"] = getattr(bot_personal_contract, "CONTRACT_REVISION", 0)
result["capability_schema_version"] = getattr(
bot_personal_contract, "BOT_PERSONAL_CAPABILITY_SCHEMA_VERSION", ""
)
result["payload_schema_version"] = getattr(
bot_personal_contract, "BOT_PERSONAL_PAYLOAD_SCHEMA_VERSION", ""
)
result["capability_state"] = "degraded"
return result
def get_token_usage_summary(self) -> dict[str, Any]:
getter = getattr(self._plugin, "token_usage_summary", None)

242
core/capability_probe.py 普通文件
查看文件

@ -0,0 +1,242 @@
"""Database-free capability state and negative-cache helpers for C4.
This module deliberately knows only about the shared bot-personal contract. It
does not inspect plugin modules, touch storage, or perform capability probing
itself; callers can use the cache to decide when a probe is worth attempting.
"""
from __future__ import annotations
import copy
import time
from collections.abc import Iterable, Mapping
from typing import Any
CAPABILITY_STATES = ("unprobed", "available", "degraded", "negative")
# Stable C4 capability profiles. Keep this tuple closed: callers may report
# only profiles understood by both companion plugins.
PROFILE_NAMES = (
"bot_schedule_current",
"bot_schedule_history",
"bot_creative",
"bot_subjective",
"locked_frame_personal",
)
_SNAPSHOT_KEYS = (
"available",
"state",
"degraded",
"pending",
"contract_fingerprint",
"contract_version",
"schema_version",
"windows",
"memory_types",
"domains",
"profiles",
"methods",
"warnings",
"error_code",
)
_MAX_ITEMS = 64
_MAX_TEXT = 256
def _text(value: object, *, limit: int = _MAX_TEXT) -> str:
try:
return str(value or "")[:limit]
except Exception:
return ""
def _unique_texts(values: object, *, allowed: set[str] | None = None) -> list[str]:
if isinstance(values, (str, bytes)) or not isinstance(values, Iterable):
return []
result: list[str] = []
try:
for value in values:
item = _text(value)
if not item or (allowed is not None and item not in allowed) or item in result:
continue
result.append(item)
if len(result) >= _MAX_ITEMS:
break
except Exception:
return result
return result
def _contract_value(module: object, name: str, default: object) -> object:
try:
return getattr(module, name)
except Exception:
return default
def _safe_contract_data(contract_module: object | None) -> dict[str, object]:
if contract_module is None:
try:
from . import bot_personal_contract as contract_module
except Exception:
contract_module = None
windows = _unique_texts(_contract_value(contract_module, "WINDOW_SLUGS", ()))
memory_types = _unique_texts(_contract_value(contract_module, "BOT_PERSONAL_MEMORY_TYPES", ()))
domain = _text(_contract_value(contract_module, "BOT_PERSONAL_MEMORY_DOMAIN", ""))
domains = [domain] if domain else []
return {
"contract_fingerprint": _text(_contract_value(contract_module, "CONTRACT_FINGERPRINT", "")),
"contract_version": _text(_contract_value(contract_module, "CONTRACT_REVISION", "")),
"schema_version": _text(
_contract_value(contract_module, "BOT_PERSONAL_CAPABILITY_SCHEMA_VERSION", "")
),
"windows": windows,
"memory_types": memory_types,
"domains": domains,
}
def _state(value: object) -> str:
candidate = _text(value).lower()
return candidate if candidate in CAPABILITY_STATES else "unprobed"
def build_capability_snapshot(
*,
available: bool = False,
state: str = "unprobed",
contract_module: object | None = None,
methods: Iterable[object] = (),
domains: Iterable[object] = (),
profiles: Iterable[object] = PROFILE_NAMES,
warnings: Iterable[object] = (),
error_code: object = "",
) -> dict[str, object]:
"""Build a bounded, JSON-safe capability snapshot."""
contract = _safe_contract_data(contract_module)
resolved_state = _state(state)
if available:
resolved_state = "available"
elif resolved_state == "available":
resolved_state = "degraded"
supplied_domains = _unique_texts(domains)
if not supplied_domains:
supplied_domains = list(contract["domains"])
result: dict[str, object] = {
"available": resolved_state == "available",
"state": resolved_state,
"degraded": resolved_state in {"degraded", "negative"},
"pending": resolved_state == "unprobed",
"contract_fingerprint": contract["contract_fingerprint"],
"contract_version": contract["contract_version"],
"schema_version": contract["schema_version"],
"windows": list(contract["windows"]),
"memory_types": list(contract["memory_types"]),
"domains": supplied_domains,
"profiles": _unique_texts(profiles, allowed=set(PROFILE_NAMES)),
"methods": _unique_texts(methods),
"warnings": _unique_texts(warnings),
"error_code": _text(error_code),
}
return {key: result[key] for key in _SNAPSHOT_KEYS}
class CapabilityCache:
"""In-memory capability state with a TTL-limited negative result."""
def __init__(self, negative_ttl: float = 60.0, clock: object | None = None) -> None:
try:
self._negative_ttl = max(0.0, float(negative_ttl))
except (TypeError, ValueError):
self._negative_ttl = 60.0
self._clock = clock if callable(clock) else time.monotonic
self._snapshot = build_capability_snapshot()
self._negative_at: float | None = None
def _now(self) -> float:
try:
return float(self._clock())
except Exception:
return time.monotonic()
def snapshot(self) -> dict[str, object]:
if self._snapshot["state"] == "negative" and self._negative_at is not None:
if self._now() - self._negative_at >= self._negative_ttl:
self._snapshot = build_capability_snapshot()
self._negative_at = None
return copy.deepcopy(self._snapshot)
def mark_available(self, snapshot: Mapping[str, object]) -> dict[str, object]:
values = dict(snapshot) if isinstance(snapshot, Mapping) else {}
self._snapshot = build_capability_snapshot(
available=True,
state="available",
methods=values.get("methods", ()),
domains=values.get("domains", ()),
profiles=values.get("profiles", PROFILE_NAMES),
warnings=values.get("warnings", ()),
error_code=values.get("error_code", ""),
contract_module=_MappingContract(values),
)
self._negative_at = None
return self.snapshot()
def mark_degraded(self, reason: object) -> dict[str, object]:
current = self.snapshot()
self._snapshot = build_capability_snapshot(
state="degraded",
methods=current["methods"],
domains=current["domains"],
profiles=current["profiles"],
warnings=[*current["warnings"], _text(reason)] if _text(reason) else current["warnings"],
error_code=_text(reason),
contract_module=_MappingContract(current),
)
self._negative_at = None
return self.snapshot()
def mark_negative(self, reason: object) -> dict[str, object]:
current = self.snapshot()
self._snapshot = build_capability_snapshot(
state="negative",
methods=current["methods"],
domains=current["domains"],
profiles=current["profiles"],
warnings=[*current["warnings"], _text(reason)] if _text(reason) else current["warnings"],
error_code=_text(reason),
contract_module=_MappingContract(current),
)
self._negative_at = self._now()
return self.snapshot()
def clear(self) -> dict[str, object]:
self._snapshot = build_capability_snapshot()
self._negative_at = None
return self.snapshot()
class _MappingContract:
"""Attribute adapter used to preserve contract metadata across cache updates."""
def __init__(self, values: Mapping[str, object]) -> None:
self.CONTRACT_FINGERPRINT = values.get("contract_fingerprint", "")
self.CONTRACT_REVISION = values.get("contract_version", "")
self.BOT_PERSONAL_CAPABILITY_SCHEMA_VERSION = values.get("schema_version", "")
self.WINDOW_SLUGS = values.get("windows", ())
self.BOT_PERSONAL_MEMORY_TYPES = values.get("memory_types", ())
domains = values.get("domains", ())
self.BOT_PERSONAL_MEMORY_DOMAIN = domains[0] if isinstance(domains, list) and domains else ""
__all__ = [
"CAPABILITY_STATES",
"PROFILE_NAMES",
"CapabilityCache",
"build_capability_snapshot",
]

332
core/profile_api.py 普通文件
查看文件

@ -0,0 +1,332 @@
"""Read-only, privacy-limited C4 Bot Profile projection.
This module deliberately operates on already-loaded records and never exposes
record content, evidence, or payload data. It is suitable as a small boundary
for later service/bridge integration.
"""
from __future__ import annotations
from collections.abc import Iterable, Mapping
import re
from typing import Any
PROFILE_NAMES = (
"bot_schedule_current",
"bot_schedule_history",
"bot_creative",
"bot_subjective",
"locked_frame_personal",
)
_PROFILE_TYPES = {
"bot_schedule_current": frozenset(
{
"bot_schedule_plan",
"bot_observed_activity",
"bot_schedule_reconciliation",
"bot_window_snapshot",
"bot_shared_activity",
"bot_detail_fragment",
"bot_calendar_event",
"bot_proactive_message",
}
),
"bot_schedule_history": frozenset(
{
"bot_schedule_plan",
"bot_observed_activity",
"bot_schedule_reconciliation",
"bot_window_snapshot",
"bot_daily_diary",
"bot_shared_activity",
"bot_detail_fragment",
"bot_calendar_event",
"bot_proactive_message",
}
),
"bot_creative": frozenset({"bot_creative_work", "bot_media_memory"}),
"bot_subjective": frozenset({"bot_daily_diary", "bot_subjective_memory"}),
"locked_frame_personal": frozenset(
{
"bot_schedule_plan",
"bot_observed_activity",
"bot_schedule_reconciliation",
"bot_window_snapshot",
"bot_daily_diary",
"bot_creative_work",
"bot_media_memory",
"bot_subjective_memory",
"bot_shared_activity",
"bot_detail_fragment",
"bot_calendar_event",
"bot_proactive_message",
}
),
}
# Public lookup is intentionally keyed by the C3 memory type. A type may be
# visible in more than one profile, while the private profile index above
# keeps selection logic straightforward.
PROFILE_MEMORY_TYPES = {
memory_type: frozenset(
profile for profile, memory_types in _PROFILE_TYPES.items() if memory_type in memory_types
)
for memory_type in sorted({item for values in _PROFILE_TYPES.values() for item in values})
}
_BOT_DOMAIN = "bot_self_schedule"
_FORBIDDEN_SCOPE_OR_VISIBILITY = {"group", "user_memory"}
_SAFE_TEXT = re.compile(r"^[^\x00-\x1f\x7f]{0,240}$")
_SENSITIVE = re.compile(
r"(?i)(?:bearer\s+|password\s*[:=]|passwd\s*[:=]|secret\s*[:=]|"
r"token\s*[:=]|api[_-]?key\s*[:=]|authorization\s*[:=]|base64,|"
r"(?:[a-z]:[\\/]|/home/|/root/|/tmp/|/var/))"
)
_SUMMARY_BY_TYPE = {
"bot_schedule_plan": "Bot schedule plan record.",
"bot_observed_activity": "Bot observed activity record.",
"bot_schedule_reconciliation": "Bot schedule reconciliation record.",
"bot_window_snapshot": "Bot window snapshot record.",
"bot_daily_diary": "Bot daily diary record.",
"bot_creative_work": "Bot creative work record.",
"bot_media_memory": "Bot media memory record.",
"bot_subjective_memory": "Bot subjective memory record.",
"bot_shared_activity": "Bot shared activity record.",
"bot_detail_fragment": "Bot schedule detail record.",
"bot_calendar_event": "Bot calendar event record.",
"bot_proactive_message": "Bot proactive message record.",
}
def profile_names() -> tuple[str, ...]:
"""Return the stable profile names without exposing mutable state."""
return PROFILE_NAMES
def profile_descriptor(profile: Any) -> dict[str, Any]:
"""Return a small, non-record descriptor for a profile name."""
name = _text(profile, 80)
return {
"profile": name,
"known": name in PROFILE_NAMES,
"memory_types": sorted(_PROFILE_TYPES.get(name, ())),
"read_only": True,
}
def _text(value: Any, limit: int = 240) -> str:
if value is None:
return ""
try:
text = str(value).strip().replace("\u3000", " ")
except Exception:
return ""
if not text or not _SAFE_TEXT.fullmatch(text[:limit]) or _SENSITIVE.search(text):
return ""
return text[:limit]
def _metadata(record: Any) -> Mapping[str, Any]:
value = getattr(record, "metadata", {})
return value if isinstance(value, Mapping) else {}
def _record_value(record: Any, metadata: Mapping[str, Any], key: str, default: Any = "") -> Any:
value = metadata.get(key, default)
if value in (None, ""):
value = getattr(record, key, default)
return value
def _is_allowed_record(record: Any, metadata: Mapping[str, Any]) -> bool:
domain = _text(metadata.get("memory_domain"), 80)
bot_personal = metadata.get("bot_personal") is True
scope = _text(getattr(record, "scope", ""), 40).lower()
visibility = _text(getattr(record, "visibility", ""), 40).lower()
metadata_scope = _text(metadata.get("scope"), 40).lower()
metadata_visibility = _text(metadata.get("visibility"), 40).lower()
if domain and domain != _BOT_DOMAIN:
return False
if not domain and not bot_personal:
return False
if scope in _FORBIDDEN_SCOPE_OR_VISIBILITY or visibility in _FORBIDDEN_SCOPE_OR_VISIBILITY:
return False
if metadata_scope in _FORBIDDEN_SCOPE_OR_VISIBILITY or metadata_visibility in _FORBIDDEN_SCOPE_OR_VISIBILITY:
return False
if domain == "user_memory" or metadata_visibility == "user_memory":
return False
return True
def _source_refs(value: Any) -> list[str]:
if isinstance(value, (str, bytes, bytearray)):
values = [value]
elif isinstance(value, Iterable):
try:
values = list(value)
except Exception:
values = []
else:
values = []
result: list[str] = []
for item in values[:16]:
safe = _text(item, 240)
if safe:
result.append(safe)
return result
def _version(value: Any) -> int:
try:
number = int(value)
except (TypeError, ValueError, OverflowError):
return 1
return max(1, min(2**31 - 1, number))
def _item(record: Any, metadata: Mapping[str, Any], memory_type: str) -> dict[str, Any]:
record_id = _text(_record_value(record, metadata, "record_id", getattr(record, "id", "")), 160)
# A bot_personal marker is an admission signal, not permission to echo an
# arbitrary caller-supplied domain into the public projection.
domain = _BOT_DOMAIN
date = _text(_record_value(record, metadata, "date"), 20)
window = _text(_record_value(record, metadata, "window"), 40)
occurred_at = _text(_record_value(record, metadata, "occurred_at"), 80)
source_kind = _text(_record_value(record, metadata, "source_kind"), 80)
evidence_level = _text(_record_value(record, metadata, "evidence_level"), 40)
status = _text(_record_value(record, metadata, "status"), 80)
return {
"record_id": record_id,
"memory_domain": domain,
"memory_type": memory_type,
"subject": "bot_self",
"date": date,
"window": window,
"occurred_at": occurred_at,
"source_kind": source_kind,
"source_refs": _source_refs(_record_value(record, metadata, "source_refs")),
"evidence_level": evidence_level,
"status": status,
"version": _version(_record_value(record, metadata, "version", 1)),
"summary": _SUMMARY_BY_TYPE.get(memory_type, "Bot personal memory record."),
"reference": f"profile:{record_id}" if record_id else "profile:unknown",
}
def _matches_query(item: Mapping[str, Any], query: str) -> bool:
needle = _text(query, 240).casefold()
if not needle:
return True
safe_fields = (
"record_id", "memory_domain", "memory_type", "subject", "date", "window",
"occurred_at", "source_kind", "evidence_level", "status", "version", "summary",
"reference",
)
haystack = " ".join(str(item.get(key, "")) for key in safe_fields)
haystack += " " + " ".join(item.get("source_refs", []))
return needle in haystack.casefold()
def _limit(value: Any) -> int:
try:
return max(1, min(100, int(value)))
except (TypeError, ValueError, OverflowError):
return 10
def build_profile_result(
records: Iterable[Any],
profile: Any,
query: str = "",
limit: int = 10,
*,
current_date: str = "",
current_window: str = "",
authorized: bool = False,
) -> dict[str, Any]:
"""Build a fixed-shape, read-only profile result without raw memory data."""
name = _text(profile, 80)
result: dict[str, Any] = {
"ok": True,
"read_only": True,
"state": "ready",
"degraded": False,
"pending": False,
"profile": name,
"items": [],
"warnings": [],
}
if name not in PROFILE_NAMES:
result.update(ok=False, state="invalid", warnings=["unknown_profile"])
return result
if name == "locked_frame_personal" and authorized is not True:
result.update(ok=False, state="forbidden", warnings=["authorization_required"])
return result
safe_date = _text(current_date, 20)
safe_window = _text(current_window, 40)
raw_query = str(query or "").strip()
safe_query = _text(query, 240)
if current_date and not safe_date:
result["warnings"].append("invalid_current_date")
if current_window and not safe_window:
result["warnings"].append("invalid_current_window")
if raw_query and not safe_query:
result["warnings"].append("unsafe_query_rejected")
return result
try:
iterable = () if records is None or isinstance(records, (str, bytes, bytearray)) else records
iterator = iter(iterable)
except Exception:
result.update(state="degraded", degraded=True, warnings=[*result["warnings"], "invalid_records"])
return result
selected: list[dict[str, Any]] = []
type_filter = _PROFILE_TYPES[name]
try:
for record in iterator:
try:
metadata = _metadata(record)
memory_type = _text(getattr(record, "memory_type", metadata.get("memory_type", "")), 80)
if not _is_allowed_record(record, metadata) or memory_type not in type_filter:
continue
date = _text(_record_value(record, metadata, "date"), 20)
window = _text(_record_value(record, metadata, "window"), 40)
if name == "bot_schedule_current":
if safe_date and date != safe_date:
continue
if safe_window and window != safe_window:
continue
elif name == "bot_schedule_history":
if safe_date and safe_window and date == safe_date and window == safe_window:
continue
item = _item(record, metadata, memory_type)
if _matches_query(item, safe_query):
selected.append(item)
except Exception:
result["warnings"].append("malformed_record_skipped")
except Exception:
result.update(state="degraded", degraded=True)
result["warnings"].append("records_iteration_failed")
selected.sort(key=lambda item: (item.get("occurred_at", ""), item.get("record_id", "")), reverse=True)
result["items"] = selected[: _limit(limit)]
if len(result["warnings"]) > 8:
result["warnings"] = result["warnings"][:8]
return result
__all__ = [
"PROFILE_NAMES",
"PROFILE_MEMORY_TYPES",
"build_profile_result",
"profile_descriptor",
"profile_names",
]

查看文件

@ -29,6 +29,7 @@ from .bot_personal_dto import (
bot_personal_payload_fingerprint,
build_bot_personal_archive,
)
from .profile_api import build_profile_result
from .chat_import import HistoricalChatImporter
from .classifier import MemoryClassifier
from .config import ConfigView
@ -832,6 +833,84 @@ class MemoryCompanionService:
})
return {"ok": True, "read_only": True, "state": "ready", "degraded": False, "pending": False, "items": items[:max(1, min(100, int(limit or 10)))]}
async def read_bot_profile(
self,
profile: str,
query: str = "",
*,
limit: int = 10,
current_date: str = "",
current_window: str = "",
authorized: bool = False,
) -> dict[str, Any]:
"""Read one C4 Bot Profile after hard domain and storage filtering."""
store = getattr(self, "store", None)
lister = getattr(store, "list_memories", None)
if not callable(lister):
return {
"ok": False,
"read_only": True,
"state": "degraded",
"degraded": True,
"pending": True,
"profile": clean_text(profile, 80),
"items": [],
"warnings": ["store_unavailable"],
}
try:
safe_limit = max(1, min(100, int(limit or 10)))
except (TypeError, ValueError):
safe_limit = 10
try:
records = await lister(
# The profile filter is applied again in profile_api; this
# bounded overscan leaves room for malformed or other-domain
# rows without scanning the whole database.
limit=min(300, max(24, safe_limit * 3)),
include_pending=False,
query="",
scope="private",
visibility="bot_self",
session_id="bot_personal",
)
except Exception:
return {
"ok": False,
"read_only": True,
"state": "degraded",
"degraded": True,
"pending": True,
"profile": clean_text(profile, 80),
"items": [],
"warnings": ["store_unavailable"],
}
if not clean_text(current_date, 20) or not clean_text(current_window, 40):
now = datetime.now(ZoneInfo("Asia/Shanghai"))
current_date = current_date or now.strftime("%Y-%m-%d")
current_window = current_window or self._bot_personal_window_for_datetime(now)
return build_profile_result(
records,
profile,
query=query,
limit=safe_limit,
current_date=current_date,
current_window=current_window,
authorized=authorized,
)
@staticmethod
def _bot_personal_window_for_datetime(value: datetime) -> str:
minutes = value.hour * 60 + value.minute
try:
from .bot_personal_contract import window_for_minutes
return window_for_minutes(minutes)
except Exception:
return ""
async def bridge_search(
self,
query: str,

查看文件

@ -54,7 +54,7 @@ class MemoryCompanionPlugin(Star):
data_dir=data_dir,
)
self.memory_companion = MemoryCompanionBridge(self.service)
self.bot_personal_capabilities = self.memory_companion.probe_bot_personal_memory_capabilities()
self.bot_personal_capabilities = self.memory_companion.probe_capability_snapshot()
if not self.bot_personal_capabilities.get("available", False):
logger.warning(
"[MemoryCompanion] Bot Personal capability probe degraded: %s",

查看文件

@ -90,6 +90,8 @@ class PluginPageApi:
("/conversation-import/pause", self.conversation_import_pause, ["POST"], "MemoryCompanion historical chat pause"),
("/conversation-import/resume", self.conversation_import_resume, ["POST"], "MemoryCompanion historical chat resume"),
("/conversation-import/rollback", self.conversation_import_rollback, ["POST"], "MemoryCompanion historical chat rollback"),
("/profiles", self.profiles, ["GET"], "MemoryCompanion Bot Profiles"),
("/capabilities/bot-personal", self.bot_personal_capabilities, ["GET"], "MemoryCompanion Bot Personal capability"),
("/companion/personal-memory", self.companion_personal_memory, ["GET"], "MemoryCompanion Page companion personal memory"),
("/companion/personal-photo", self.companion_personal_photo, ["GET"], "MemoryCompanion Page companion personal photo"),
("/companion/personal-photo-data", self.companion_personal_photo_data, ["GET"], "MemoryCompanion Page companion personal photo data"),
@ -112,6 +114,35 @@ class PluginPageApi:
stats.pop("pending_review", None)
return self._ok({"stats": stats})
async def profiles(self):
profile = clean_text(request.args.get("profile", ""), 80)
query = clean_text(request.args.get("query", ""), 240)
current_date = clean_text(request.args.get("date", ""), 20)
current_window = clean_text(request.args.get("window", ""), 40)
try:
limit = max(1, min(100, int(request.args.get("limit", "10"))))
except (TypeError, ValueError):
limit = 10
result = await self.plugin.service.read_bot_profile(
profile,
query=query,
limit=limit,
current_date=current_date,
current_window=current_window,
authorized=False,
)
return self._ok({"result": result})
async def bot_personal_capabilities(self):
getter = getattr(self.plugin, "bot_personal_capability_status", None)
result = getter() if callable(getter) else {
"available": False,
"state": "degraded",
"degraded": True,
"warnings": ["capability_status_unavailable"],
}
return self._ok({"result": result if isinstance(result, dict) else {}})
async def operations_diagnostics(self):
try:
return self._ok({"diagnostics": await self.plugin.service.operational_report()})

查看文件

@ -0,0 +1,93 @@
from __future__ import annotations
from core import bot_personal_contract
from core.capability_probe import (
CAPABILITY_STATES,
PROFILE_NAMES,
CapabilityCache,
build_capability_snapshot,
)
def test_snapshot_uses_contract_fingerprint_windows_and_types():
snapshot = build_capability_snapshot()
assert snapshot["contract_fingerprint"] == bot_personal_contract.CONTRACT_FINGERPRINT
assert snapshot["windows"] == list(bot_personal_contract.WINDOW_SLUGS)
assert snapshot["memory_types"] == list(bot_personal_contract.BOT_PERSONAL_MEMORY_TYPES)
assert set(snapshot) == {
"available", "state", "degraded", "pending", "contract_fingerprint",
"contract_version", "schema_version", "windows", "memory_types", "domains",
"profiles", "methods", "warnings", "error_code",
}
def test_initial_cache_is_unprobed_and_pending():
snapshot = CapabilityCache().snapshot()
assert snapshot["state"] == "unprobed"
assert snapshot["pending"] is True
assert snapshot["available"] is False
def test_available_degraded_and_negative_states_are_observable():
cache = CapabilityCache()
available = cache.mark_available({"methods": ["search", "search"], "profiles": PROFILE_NAMES})
assert available["state"] == "available"
assert available["available"] is True
assert available["degraded"] is False
degraded = cache.mark_degraded("contract_invalid")
assert degraded["state"] == "degraded"
assert degraded["degraded"] is True
assert degraded["error_code"] == "contract_invalid"
negative = cache.mark_negative("bridge_missing")
assert negative["state"] == "negative"
assert negative["available"] is False
assert negative["pending"] is False
assert negative["degraded"] is True
assert negative["error_code"] == "bridge_missing"
def test_negative_ttl_returns_to_unprobed_after_expiry():
now = [10.0]
cache = CapabilityCache(negative_ttl=5, clock=lambda: now[0])
cache.mark_negative("not_ready")
assert cache.snapshot()["state"] == "negative"
now[0] = 14.99
assert cache.snapshot()["state"] == "negative"
now[0] = 15.0
assert cache.snapshot()["state"] == "unprobed"
def test_snapshot_is_a_deep_copy():
cache = CapabilityCache()
value = cache.snapshot()
value["windows"].append("mutated")
value["warnings"].append("mutated")
fresh = cache.snapshot()
assert "mutated" not in fresh["windows"]
assert "mutated" not in fresh["warnings"]
def test_malformed_contract_module_is_safe():
class Broken:
def __getattribute__(self, _name):
raise RuntimeError("broken contract")
snapshot = build_capability_snapshot(
available=True,
contract_module=Broken(),
profiles=[*PROFILE_NAMES, "not-a-profile", "not-a-profile"],
methods=["search", "search", object()],
)
assert snapshot["state"] == "available"
assert snapshot["available"] is True
assert snapshot["profiles"] == list(PROFILE_NAMES)
assert all(isinstance(value, (str, int, float, bool, list, dict)) or value is None
for value in snapshot.values())
def test_public_state_constants_are_closed():
assert set(CAPABILITY_STATES) == {"unprobed", "available", "degraded", "negative"}
assert len(PROFILE_NAMES) == 5
assert len(set(PROFILE_NAMES)) == 5

110
tests/test_c4_profile_api.py 普通文件
查看文件

@ -0,0 +1,110 @@
from dataclasses import dataclass, field
from core.profile_api import PROFILE_MEMORY_TYPES, PROFILE_NAMES, build_profile_result
@dataclass
class Record:
id: str
memory_type: str
metadata: dict = field(default_factory=dict)
scope: str = "private"
visibility: str = "bot_self"
content: str = "RAW CONTENT MUST NOT ESCAPE"
evidence: str = "RAW EVIDENCE MUST NOT ESCAPE"
occurred_at: str = ""
def rec(record_id, memory_type, *, date="2026-07-30", window="afternoon", domain="bot_self_schedule", **metadata):
data = {
"memory_domain": domain,
"date": date,
"window": window,
"occurred_at": f"{date}T15:00:00+08:00",
"source_kind": "observed",
"source_refs": [f"safe:{record_id}"],
"evidence_level": "L2",
"status": "active",
"version": 1,
}
data.update(metadata)
return Record(record_id, memory_type, data)
def test_contract_exports_and_five_profiles_have_fixed_shape():
assert PROFILE_NAMES == (
"bot_schedule_current", "bot_schedule_history", "bot_creative",
"bot_subjective", "locked_frame_personal",
)
assert {
"bot_schedule_plan", "bot_observed_activity", "bot_schedule_reconciliation",
"bot_window_snapshot", "bot_daily_diary", "bot_creative_work", "bot_media_memory",
"bot_subjective_memory", "bot_shared_activity", "bot_detail_fragment",
"bot_calendar_event", "bot_proactive_message",
}.issubset(PROFILE_MEMORY_TYPES)
assert set(PROFILE_MEMORY_TYPES["bot_creative_work"]) == {"bot_creative", "locked_frame_personal"}
records = [
rec("plan", "bot_schedule_plan"), rec("creative", "bot_creative_work"),
rec("subjective", "bot_subjective_memory"),
]
for profile in PROFILE_NAMES[:4]:
result = build_profile_result(records, profile, current_date="2026-07-30", current_window="afternoon")
assert set(result) == {"ok", "read_only", "state", "degraded", "pending", "profile", "items", "warnings"}
assert result["read_only"] is True
def test_bot_user_group_isolation_and_bot_personal_flag():
records = [
rec("bot", "bot_observed_activity"),
rec("user", "bot_observed_activity", domain="user_memory"),
rec("group", "bot_observed_activity", domain="bot_self_schedule", scope="group", visibility="group"),
Record("flag", "bot_observed_activity", {"bot_personal": True, "date": "2026-07-30", "window": "afternoon"}),
]
result = build_profile_result(records, "bot_schedule_current", current_date="2026-07-30", current_window="afternoon")
assert {item["record_id"] for item in result["items"]} == {"bot", "flag"}
def test_current_and_history_partition_by_date_and_window():
records = [
rec("current", "bot_schedule_plan", date="2026-07-30", window="afternoon"),
rec("old-window", "bot_schedule_plan", date="2026-07-30", window="morning"),
rec("old-date", "bot_schedule_plan", date="2026-07-29", window="afternoon"),
]
current = build_profile_result(records, "bot_schedule_current", current_date="2026-07-30", current_window="afternoon")
history = build_profile_result(records, "bot_schedule_history", current_date="2026-07-30", current_window="afternoon")
assert [item["record_id"] for item in current["items"]] == ["current"]
assert {item["record_id"] for item in history["items"]} == {"old-window", "old-date"}
def test_locked_profile_requires_authorization_and_returns_all_bot_types_when_allowed():
records = [rec("creative", "bot_creative_work"), rec("subjective", "bot_subjective_memory")]
denied = build_profile_result(records, "locked_frame_personal", authorized=False)
allowed = build_profile_result(records, "locked_frame_personal", authorized=True)
assert denied["ok"] is False and denied["state"] == "forbidden" and denied["items"] == []
assert {item["record_id"] for item in allowed["items"]} == {"creative", "subjective"}
def test_no_payload_content_or_evidence_leak_and_query_only_safe_fields():
record = rec("safe", "bot_creative_work", summary="SECRET PAYLOAD SHOULD NOT BE USED")
result = build_profile_result([record], "bot_creative", query="creative work")
assert len(result["items"]) == 1
item = result["items"][0]
assert set(item) == {
"record_id", "memory_domain", "memory_type", "subject", "date", "window", "occurred_at",
"source_kind", "source_refs", "evidence_level", "status", "version", "summary", "reference",
}
assert "RAW CONTENT" not in str(result)
assert "RAW EVIDENCE" not in str(result)
assert "SECRET PAYLOAD" not in str(result)
assert build_profile_result([record], "bot_creative", query="RAW CONTENT")["items"] == []
def test_unknown_profile_limit_and_malformed_input_degrade_safely():
invalid = build_profile_result([], "unknown")
assert invalid["ok"] is False and invalid["state"] == "invalid"
assert len(build_profile_result([rec(str(i), "bot_creative_work") for i in range(120)], "bot_creative", limit=100)["items"]) == 100
assert len(build_profile_result([rec("one", "bot_creative_work")], "bot_creative", limit="bad")["items"]) == 1
malformed = build_profile_result(None, "bot_creative")
assert malformed["state"] == "ready" and malformed["items"] == []
malformed_records = build_profile_result(42, "bot_creative")
assert malformed_records["state"] == "degraded" and malformed_records["ok"] is True

查看文件

@ -0,0 +1,124 @@
from __future__ import annotations
import asyncio
from pathlib import Path
from core.bridge import MemoryCompanionBridge
from core.models import MemoryRecord
from core.service import MemoryCompanionService
from core.store import MemoryStore
def run(coro):
return asyncio.run(coro)
def make_service(tmp_path: Path):
service = object.__new__(MemoryCompanionService)
service.store = MemoryStore(tmp_path / "memory.db")
service.store.initialize()
service._schedule_memory_embedding = lambda *args, **kwargs: None
return service
def envelope(memory_type: str, key: str, *, date: str = "2026-07-30", window: str = "afternoon"):
return {
"memory_type": memory_type,
"date": date,
"window": window,
"occurred_at": f"{date}T15:00:00+08:00",
"source_refs": [f"companion:c4:{key}"],
"idempotency_key": f"c4:{key}",
"payload": {"summary": "private raw payload must stay hidden", "topic": key},
}
def test_service_profiles_partition_domains_and_hide_raw_fields(tmp_path):
service = make_service(tmp_path)
try:
current = run(service.record_bot_personal_archive(envelope("bot_schedule_plan", "current")))
old_window = run(service.record_bot_personal_archive(envelope("bot_schedule_plan", "old-window", window="morning")))
old_date = run(service.record_bot_personal_archive(envelope("bot_schedule_plan", "old-date", date="2026-07-29")))
creative = run(service.record_bot_personal_archive(envelope("bot_creative_work", "creative")))
subjective = run(service.record_bot_personal_archive(envelope("bot_subjective_memory", "subjective")))
run(service.store.insert_memory(MemoryRecord(
id="user-leak",
memory_type="bot_creative_work",
scope="private",
session_id="bot_personal",
visibility="bot_self",
content="raw user content",
metadata={"memory_domain": "user_memory", "bot_personal": True},
)))
run(service.store.insert_memory(MemoryRecord(
id="group-leak",
memory_type="bot_creative_work",
scope="group",
session_id="bot_personal",
visibility="group",
content="raw group content",
metadata={"memory_domain": "bot_self_schedule", "bot_personal": True},
)))
current_profile = run(service.read_bot_profile(
"bot_schedule_current",
current_date="2026-07-30",
current_window="afternoon",
))
history_profile = run(service.read_bot_profile(
"bot_schedule_history",
current_date="2026-07-30",
current_window="afternoon",
))
creative_profile = run(service.read_bot_profile("bot_creative", query="creative"))
subjective_profile = run(service.read_bot_profile("bot_subjective"))
locked_denied = run(service.read_bot_profile("locked_frame_personal"))
locked_allowed = run(service.read_bot_profile("locked_frame_personal", authorized=True))
unsafe_query = run(service.read_bot_profile("bot_creative", query="C:\\private\\secret.txt"))
assert [item["record_id"] for item in current_profile["items"]] == [current["record_id"]]
assert {item["record_id"] for item in history_profile["items"]} == {old_window["record_id"], old_date["record_id"]}
assert [item["record_id"] for item in creative_profile["items"]] == [creative["record_id"]]
assert [item["record_id"] for item in subjective_profile["items"]] == [subjective["record_id"]]
assert locked_denied["state"] == "forbidden" and locked_denied["items"] == []
assert {item["record_id"] for item in locked_allowed["items"]} == {
current["record_id"], old_window["record_id"], old_date["record_id"],
creative["record_id"], subjective["record_id"],
}
assert unsafe_query["items"] == []
assert "unsafe_query_rejected" in unsafe_query["warnings"]
for result in (current_profile, history_profile, creative_profile, subjective_profile, locked_allowed):
for item in result["items"]:
assert "payload" not in item and "content" not in item and "evidence" not in item
assert "private raw payload" not in str(item)
assert "user-leak" not in str(locked_allowed)
assert "group-leak" not in str(locked_allowed)
finally:
service.store.close()
def test_bridge_profile_and_capability_compatibility(tmp_path):
service = make_service(tmp_path)
try:
run(service.record_bot_personal_archive(envelope("bot_creative_work", "bridge")))
bridge = MemoryCompanionBridge(service)
snapshot = bridge.probe_capability_snapshot()
legacy = bridge.probe_bot_personal_memory_capabilities()
profile = run(bridge.read_bot_profile("bot_creative", query="bridge"))
missing = run(MemoryCompanionBridge(object()).read_bot_profile("bot_creative"))
assert snapshot["state"] == "available"
assert snapshot["capability_state"] == "available"
assert snapshot["profiles"] == [
"bot_schedule_current", "bot_schedule_history", "bot_creative",
"bot_subjective", "locked_frame_personal",
]
assert legacy["state"] == "ready"
assert legacy["capability_state"] == "available"
assert profile["ok"] and profile["read_only"] is True
assert len(profile["items"]) == 1
assert missing["state"] == "degraded"
assert missing["error_code"] == "bridge_method_unavailable"
finally:
service.store.close()