astrbot_plugin_private_comp.../p5c_gate.py

77 行
2.4 KiB
Python

"""P5-C default-off, deny-only guards for confirmed Companion sinks.
This plugin does not receive a host-issued opaque invocation for raw media,
background group vision, context-image captioning, or LLM tool parameters.
Consequently an enabled P5-C flag can only deny its matching sink. No event,
P3 carrier, session, dict, JSON payload, or P5-B handle is ever inspected as
an allow credential.
"""
from __future__ import annotations
import hashlib
import logging
from typing import Any
logger = logging.getLogger("PrivateCompanion.P5C")
RAW_IMAGE_PROVIDER_SINK = "raw_image_provider"
GROUP_IMAGE_VISION_SINK = "group_image_vision"
CONTEXT_IMAGE_CAPTION_SINK = "context_image_caption"
TOOL_PARAMETER_SINK = "tool_parameter"
P5C_CONTROLLED_DENY_REASON = "p5c_host_opaque_invocation_unavailable"
P5C_SINK_FLAGS = {
RAW_IMAGE_PROVIDER_SINK: "enable_p5c_raw_image_provider_gate",
GROUP_IMAGE_VISION_SINK: "enable_p5c_group_image_vision_gate",
CONTEXT_IMAGE_CAPTION_SINK: "enable_p5c_context_image_caption_gate",
TOOL_PARAMETER_SINK: "enable_p5c_tool_parameter_gate",
}
def p5c_gate_allows(host: Any, sink: str) -> bool:
"""Return false only for an enabled, confirmed P5-C sink.
The audit projection is derived solely from the static sink name. It
deliberately avoids reading any event, request, source, media, tool
parameter, provider payload, or caller-provided identity object.
"""
flag = P5C_SINK_FLAGS.get(sink)
if not flag:
raise ValueError("p5c_unknown_sink")
try:
enabled = bool(getattr(host, flag, False))
except Exception:
enabled = True
if not enabled:
return True
audit = {
"sink": sink,
"outcome": "denied",
"reason_code": P5C_CONTROLLED_DENY_REASON,
"ref_hash": hashlib.sha256(f"p5c:{sink}".encode("utf-8")).hexdigest(),
}
try:
entries = getattr(host, "_p5c_denial_audit", None)
if not isinstance(entries, list):
entries = []
setattr(host, "_p5c_denial_audit", entries)
entries.append(audit)
del entries[:-64]
except Exception:
pass
logger.warning("[PrivateCompanion][P5-C] gate=%s", audit)
return False
__all__ = [
"CONTEXT_IMAGE_CAPTION_SINK",
"GROUP_IMAGE_VISION_SINK",
"P5C_CONTROLLED_DENY_REASON",
"P5C_SINK_FLAGS",
"RAW_IMAGE_PROVIDER_SINK",
"TOOL_PARAMETER_SINK",
"p5c_gate_allows",
]