astrbot_plugin_private_comp.../ops_status_projection.py

204 行
8 KiB
Python

"""Safe, versioned status DTOs for the Companion management page.
These projections deliberately expose only fixed labels, switches and bounded
counts. They must not become another path to P4 ledgers, P5 source material,
P3 carriers, review records or unified-person identities.
"""
from __future__ import annotations
from typing import Any
try: # Package import for the plugin; direct import for focused tests.
from .context_orchestration import CONTRACT_VERSION as P3_CONTRACT_VERSION
from .p4_live_runtime import project_warmth_band
from .p6_readonly_projection import build_p6_readonly_status
except ImportError: # pragma: no cover - exercised by unittest discovery.
from context_orchestration import CONTRACT_VERSION as P3_CONTRACT_VERSION # type: ignore
from p4_live_runtime import project_warmth_band # type: ignore
from p6_readonly_projection import build_p6_readonly_status # type: ignore
OPS_STATUS_SCHEMA_VERSION = "ops.page_status.v1"
P4_USER_STATUS_SCHEMA_VERSION = "ops.page_user_p4_status.v1"
_MAX_REVIEW_COUNT = 10_000_000
_P5_GROUP_FLAGS = {
"b": (
"enable_p5_b1_recall_gate",
"enable_p5_b1_bridge_gate",
"enable_p5_b1_forward_quote_prompt_gate",
"enable_p5_b1_private_reply_vision_gate",
),
"c": (
"enable_p5c_raw_image_provider_gate",
"enable_p5c_group_image_vision_gate",
"enable_p5c_context_image_caption_gate",
"enable_p5c_tool_parameter_gate",
),
"d": (
"enable_p5d_cross_turn_correlation",
"enable_p5d_manual_recovery",
),
}
_WARMTH_BANDS = ("unknown", "confinement", "guarded", "reserved", "neutral", "warm", "close", "very_warm")
def build_ops_status_projection(
*,
p4_runtime_enabled: Any,
p4_manual_review_enabled: Any,
p4_review_container: Any,
p4_effect_container: Any,
p5_source_observer_enabled: Any,
p5_protection_flags: Any,
p6_registry_status: Any,
) -> dict[str, Any]:
"""Return the exact, page-safe cross-stage status DTO.
Callers may pass the internal P4 review container and registry status, but
this function reads neither candidate records nor any ledger/audit body.
The returned structure is allowlist-only and contains no identifiers,
scores, source content, capabilities, attestations or credentials.
"""
runtime_enabled = bool(p4_runtime_enabled)
review_enabled = bool(p4_manual_review_enabled)
review = _review_queue_summary(p4_review_container, enabled=review_enabled)
runtime = _p4_runtime_aggregate(p4_effect_container, enabled=runtime_enabled)
p5_group_counts = _p5_group_counts(p5_protection_flags)
protected_sink_count = sum(p5_group_counts.values())
observer_enabled = bool(p5_source_observer_enabled)
if protected_sink_count:
p5_status = "protected"
elif observer_enabled:
p5_status = "observing"
else:
p5_status = "disabled"
p6 = build_p6_readonly_status(p6_registry_status)
return {
"schema_version": OPS_STATUS_SCHEMA_VERSION,
"p3": {
"contract_version": P3_CONTRACT_VERSION,
"status": "available",
},
"p4": {
"runtime_enabled": runtime_enabled,
"protection_status": "protected" if runtime_enabled else "disabled",
"review_enabled": review_enabled,
"review_candidate_count": review["candidate_count"],
"review_status": review["status"],
"runtime_status": runtime["status"],
"warmth_band_counts": dict(runtime["warmth_band_counts"]),
"active_confinement_count": runtime["active_confinement_count"],
},
"p5": {
"source_observer_enabled": observer_enabled,
"b_gate_count": p5_group_counts["b"],
"c_gate_count": p5_group_counts["c"],
"d_gate_count": p5_group_counts["d"],
"protected_sink_count": protected_sink_count,
"protection_status": p5_status,
},
"p6": {
"health": p6["health"],
"reason_code": p6["reason_code"],
"counts": dict(p6["counts"]),
},
}
def build_user_p4_status_projection(*, runtime_enabled: Any) -> dict[str, Any]:
"""Return a user-page P4 status without attempting identity resolution.
A web page has no authenticated AstrBot event principal. It therefore
cannot safely bind a stored user record to a P4 person or issue a review
decision. The explicit unavailable state is safer than guessing from an
ID, alias, nickname, ledger, or configured owner list.
"""
return {
"schema_version": P4_USER_STATUS_SCHEMA_VERSION,
"runtime_enabled": bool(runtime_enabled),
"warmth_band": "unknown",
"confinement_state": "unknown",
"confinement_until": "",
"review_candidate_count": 0,
"review_status": "identity_not_verifiably_linked",
"action_available": False,
"action_unavailable_reason": "host_principal_unavailable",
}
def _review_queue_summary(container: Any, *, enabled: bool) -> dict[str, Any]:
if not enabled:
return {"candidate_count": 0, "status": "disabled"}
if container is None:
return {"candidate_count": 0, "status": "no_pending"}
if type(container) is not dict or container.get("version") != 1:
return {"candidate_count": 0, "status": "unverifiable"}
candidates = container.get("candidates")
decisions = container.get("decisions")
event_index = container.get("event_index")
if type(candidates) is not dict or type(decisions) is not dict or type(event_index) is not dict:
return {"candidate_count": 0, "status": "unverifiable"}
if len(candidates) > _MAX_REVIEW_COUNT or len(decisions) > _MAX_REVIEW_COUNT:
return {"candidate_count": 0, "status": "unverifiable"}
pending_count = sum(1 for candidate_id in candidates if candidate_id not in decisions)
return {
"candidate_count": pending_count,
"status": "pending" if pending_count else "no_pending",
}
def _p5_group_counts(value: Any) -> dict[str, int]:
if type(value) is not dict:
return {group: 0 for group in _P5_GROUP_FLAGS}
return {
group: sum(bool(value.get(key)) for key in flags)
for group, flags in _P5_GROUP_FLAGS.items()
}
def _p4_runtime_aggregate(container: Any, *, enabled: bool) -> dict[str, Any]:
counts = {band: 0 for band in _WARMTH_BANDS}
if not enabled:
return {"status": "disabled", "warmth_band_counts": counts, "active_confinement_count": 0}
if container is None:
return {"status": "no_runtime_data", "warmth_band_counts": counts, "active_confinement_count": 0}
if type(container) is not dict or type(container.get("people")) is not dict:
return {"status": "unverifiable", "warmth_band_counts": counts, "active_confinement_count": 0}
people = container["people"]
if len(people) > _MAX_REVIEW_COUNT:
return {"status": "unverifiable", "warmth_band_counts": counts, "active_confinement_count": 0}
active_confinement_count = 0
invalid_count = 0
for entry in people.values():
state = entry.get("state") if type(entry) is dict else None
if type(state) is not dict:
invalid_count += 1
continue
working = dict(state)
if working.get("mode") == "effect_preparation":
working["mode"] = "shadow"
try:
band = project_warmth_band(working).get("warmth_band")
except Exception:
band = "unknown"
if band not in counts:
band = "unknown"
counts[band] += 1
if band == "confinement":
active_confinement_count += 1
if invalid_count:
counts["unknown"] += invalid_count
return {
"status": "degraded" if invalid_count else "ready",
"warmth_band_counts": counts,
"active_confinement_count": active_confinement_count,
}
__all__ = [
"OPS_STATUS_SCHEMA_VERSION",
"P4_USER_STATUS_SCHEMA_VERSION",
"build_ops_status_projection",
"build_user_p4_status_projection",
]