emergent-security/src/emergent_security/memory.py

33 lines
1 KiB
Python

"""Security memory (canon B5). Memory informs; negative memory blocks until cleared (C10)."""
from __future__ import annotations
from dataclasses import dataclass
from typing import Any, Mapping
from emergent_security.model import Request, frozen_mapping
@dataclass(frozen=True)
class MemoryEntry:
decision_class: str
scope: Mapping[str, Any]
polarity: str
cleared: bool = False
@classmethod
def from_dict(cls, data: Mapping[str, Any]) -> MemoryEntry:
return cls(
decision_class=str(data["decision_class"]),
scope=frozen_mapping(data.get("scope")),
polarity=str(data.get("polarity", "positive")),
cleared=bool(data.get("cleared", False)),
)
def blocks(self, request: Request) -> bool:
return (
self.polarity == "negative"
and not self.cleared
and self.decision_class == request.decision_class
and all(request.scope.get(key) == value for key, value in self.scope.items())
)