feat(p6): add read-only status projection contract
这个提交存在于:
父节点
f6babf2c1e
当前提交
56e7517508
共有 2 个文件被更改,包括 88 次插入 和 0 次删除
47
core/p6_readonly_projection.py
普通文件
47
core/p6_readonly_projection.py
普通文件
|
|
@ -0,0 +1,47 @@
|
|||
"""P6-A minimal read-only status projection for the Memory consumer."""
|
||||
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 bounded counts without accepting P4 or authorization authority."""
|
||||
# `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": "memory_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,41 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
|
||||
from core.p6_readonly_projection import (
|
||||
P6_READONLY_STATUS_FINGERPRINT,
|
||||
P6_READONLY_STATUS_SCHEMA,
|
||||
build_p6_readonly_status,
|
||||
)
|
||||
|
||||
|
||||
class P6ReadOnlyProjectionTests(unittest.TestCase):
|
||||
def test_only_bounded_counts_and_static_contract_are_returned(self) -> None:
|
||||
result = 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("memory_companion", result["source_plugin"])
|
||||
self.assertEqual(P6_READONLY_STATUS_SCHEMA, result["schema_version"])
|
||||
self.assertEqual(P6_READONLY_STATUS_FINGERPRINT, result["contract_fingerprint"])
|
||||
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 = 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"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
正在加载…
添加表格
在新工单中引用