feat(p6): add read-only status projection contract
这个提交存在于:
父节点
e922ac7210
当前提交
d7e8357898
共有 3 个文件被更改,包括 111 次插入 和 0 次删除
8
main.py
8
main.py
|
|
@ -141,6 +141,7 @@ from .p5_ingress_firewall import evaluate_ingress_segments
|
|||
from .p5_provenance_attestation import FIREWALL_STATUSES, P5AttestationRegistry
|
||||
from .p5c_gate import RAW_IMAGE_PROVIDER_SINK, p5c_gate_allows
|
||||
from .p5d_security_events import p5d_security_event_allows
|
||||
from .p6_readonly_projection import build_p6_readonly_status
|
||||
from .segmented_message import flatten_component_chunks, split_plain_component_chain_detailed
|
||||
from .token_budget import TokenBudgetMixin
|
||||
from .balance_awareness import BalanceAwarenessMixin
|
||||
|
|
@ -281,6 +282,13 @@ class PrivateCompanionExtensionAPI:
|
|||
def get_unified_person_registry_status(self) -> dict[str, Any]:
|
||||
return self._plugin._unified_person_registry_status()
|
||||
|
||||
def get_p6_readonly_status(self) -> dict[str, Any]:
|
||||
"""Expose P6-A counts only; it has no mutation or authority surface."""
|
||||
try:
|
||||
return build_p6_readonly_status(self._plugin._unified_person_registry_status())
|
||||
except Exception:
|
||||
return build_p6_readonly_status(None)
|
||||
|
||||
def get_unified_person_audit_events(self, *, limit: int = 100) -> list[dict[str, Any]]:
|
||||
return self._plugin._unified_person_audit_events(limit=limit)
|
||||
|
||||
|
|
|
|||
51
p6_readonly_projection.py
普通文件
51
p6_readonly_projection.py
普通文件
|
|
@ -0,0 +1,51 @@
|
|||
"""P6-A minimal read-only status projection for the Companion authority."""
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
from typing import Any
|
||||
|
||||
|
||||
P6_READONLY_STATUS_SCHEMA = "ops.p6.read_only_status.v1"
|
||||
_STATUS_FIELDS = ("profiles", "identity_links", "audit_events", "operations")
|
||||
_HEALTH = frozenset({"ready", "degraded", "unverifiable"})
|
||||
|
||||
|
||||
def _fingerprint() -> str:
|
||||
payload = {"schema_version": P6_READONLY_STATUS_SCHEMA, "count_fields": _STATUS_FIELDS}
|
||||
encoded = json.dumps(payload, ensure_ascii=True, separators=(",", ":"), sort_keys=True).encode("utf-8")
|
||||
return hashlib.sha256(encoded).hexdigest()
|
||||
|
||||
|
||||
P6_READONLY_STATUS_FINGERPRINT = _fingerprint()
|
||||
|
||||
|
||||
def _count(value: Any) -> int:
|
||||
return value if type(value) is int and 0 <= value <= 10_000_000 else 0
|
||||
|
||||
|
||||
def build_p6_readonly_status(status: Any, *, health: str = "ready", reason_code: str = "") -> dict[str, Any]:
|
||||
"""Project only bounded counts and a static health reason.
|
||||
|
||||
The input may be a registry status object but no profile, identity, P4
|
||||
event, audit body, carrier, capability, or authorization is returned.
|
||||
"""
|
||||
# `isinstance` can invoke an untrusted object's ``__class__`` hook; P6-A
|
||||
# accepts only a concrete internal dict and otherwise fails closed.
|
||||
source = status if type(status) is dict else {}
|
||||
normalized_health = health if health in _HEALTH else "unverifiable"
|
||||
normalized_reason = reason_code if isinstance(reason_code, str) and len(reason_code) <= 80 else "invalid_reason_code"
|
||||
if type(status) is not dict:
|
||||
normalized_health = "unverifiable"
|
||||
normalized_reason = "registry_status_unavailable"
|
||||
return {
|
||||
"schema_version": P6_READONLY_STATUS_SCHEMA,
|
||||
"source_plugin": "private_companion",
|
||||
"contract_fingerprint": P6_READONLY_STATUS_FINGERPRINT,
|
||||
"health": normalized_health,
|
||||
"reason_code": normalized_reason,
|
||||
"counts": {field: _count(source.get(field)) for field in _STATUS_FIELDS},
|
||||
}
|
||||
|
||||
|
||||
__all__ = ["P6_READONLY_STATUS_SCHEMA", "P6_READONLY_STATUS_FINGERPRINT", "build_p6_readonly_status"]
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import importlib.util
|
||||
from pathlib import Path
|
||||
import unittest
|
||||
|
||||
|
||||
MODULE_PATH = Path(__file__).resolve().parents[1] / "p6_readonly_projection.py"
|
||||
SPEC = importlib.util.spec_from_file_location("p6_readonly_projection_under_test", MODULE_PATH)
|
||||
assert SPEC and SPEC.loader
|
||||
projection = importlib.util.module_from_spec(SPEC)
|
||||
SPEC.loader.exec_module(projection)
|
||||
|
||||
|
||||
class P6ReadOnlyProjectionTests(unittest.TestCase):
|
||||
def test_only_bounded_counts_and_static_contract_are_returned(self) -> None:
|
||||
result = projection.build_p6_readonly_status({
|
||||
"profiles": 3,
|
||||
"identity_links": 4,
|
||||
"audit_events": 5,
|
||||
"operations": 6,
|
||||
"person_id": "must-not-leak",
|
||||
"p4_event": {"body": "must-not-leak"},
|
||||
"capability": "must-not-leak",
|
||||
})
|
||||
self.assertEqual("private_companion", result["source_plugin"])
|
||||
self.assertEqual({"profiles": 3, "identity_links": 4, "audit_events": 5, "operations": 6}, result["counts"])
|
||||
self.assertEqual({"schema_version", "source_plugin", "contract_fingerprint", "health", "reason_code", "counts"}, set(result))
|
||||
self.assertNotIn("must-not-leak", repr(result))
|
||||
|
||||
def test_invalid_status_fails_closed_without_reading_object(self) -> None:
|
||||
class Poison:
|
||||
def __getattribute__(self, name):
|
||||
raise AssertionError(name)
|
||||
result = projection.build_p6_readonly_status(Poison())
|
||||
self.assertEqual("unverifiable", result["health"])
|
||||
self.assertEqual("registry_status_unavailable", result["reason_code"])
|
||||
self.assertEqual({"profiles": 0, "identity_links": 0, "audit_events": 0, "operations": 0}, result["counts"])
|
||||
|
||||
def test_memory_consumer_uses_the_same_frozen_contract_fingerprint(self) -> None:
|
||||
memory_path = Path(__file__).resolve().parents[2] / "astrbot_plugin_memory_companion-main" / "core" / "p6_readonly_projection.py"
|
||||
spec = importlib.util.spec_from_file_location("p6_readonly_projection_memory_under_test", memory_path)
|
||||
self.assertIsNotNone(spec)
|
||||
self.assertIsNotNone(spec.loader)
|
||||
memory = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(memory)
|
||||
self.assertEqual(projection.P6_READONLY_STATUS_SCHEMA, memory.P6_READONLY_STATUS_SCHEMA)
|
||||
self.assertEqual(projection.P6_READONLY_STATUS_FINGERPRINT, memory.P6_READONLY_STATUS_FINGERPRINT)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
正在加载…
添加表格
在新工单中引用