governed-objects 0.2.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- governed_objects/__init__.py +7 -0
- governed_objects/_version.py +38 -0
- governed_objects/adapters/base.py +80 -0
- governed_objects/adapters/context.py +58 -0
- governed_objects/adapters/mandate.py +45 -0
- governed_objects/adapters/registry.py +25 -0
- governed_objects/agent/session.py +111 -0
- governed_objects/applications/registry.py +39 -0
- governed_objects/authority/rules.py +63 -0
- governed_objects/delegation/configuration.py +74 -0
- governed_objects/delegation/mandate.py +44 -0
- governed_objects/delegation/materializer.py +152 -0
- governed_objects/delegation/projection.py +48 -0
- governed_objects/delegation/resolver.py +180 -0
- governed_objects/delegation/scope.py +75 -0
- governed_objects/delegation/service.py +245 -0
- governed_objects/delegation/validator.py +159 -0
- governed_objects/domain/__init__.py +21 -0
- governed_objects/domain/actor_manifest.py +40 -0
- governed_objects/domain/actors.py +55 -0
- governed_objects/domain/clock.py +28 -0
- governed_objects/domain/command.py +95 -0
- governed_objects/domain/command_context.py +35 -0
- governed_objects/domain/models.py +110 -0
- governed_objects/domain/traversal.py +25 -0
- governed_objects/events/lineage.py +71 -0
- governed_objects/events/reducers/__init__.py +2 -0
- governed_objects/events/reducers/mandate.py +31 -0
- governed_objects/events/replay.py +208 -0
- governed_objects/idempotency/store.py +160 -0
- governed_objects/interchange/__init__.py +49 -0
- governed_objects/interchange/artifact.py +310 -0
- governed_objects/interchange/export.py +345 -0
- governed_objects/interchange/governance.py +26 -0
- governed_objects/interchange/importer.py +672 -0
- governed_objects/interchange/prc.py +76 -0
- governed_objects/interchange/types.py +228 -0
- governed_objects/llm/__init__.py +16 -0
- governed_objects/llm/loop.py +130 -0
- governed_objects/llm/model.py +44 -0
- governed_objects/llm/tools/registry.py +87 -0
- governed_objects/llm/tools/results.py +53 -0
- governed_objects/llm/tools/schemas.py +67 -0
- governed_objects/llm/tools/surface.py +252 -0
- governed_objects/llm/trace.py +58 -0
- governed_objects/materialization/service.py +107 -0
- governed_objects/observations/__init__.py +3 -0
- governed_objects/observations/ingestion.py +375 -0
- governed_objects/observations/mapping.py +48 -0
- governed_objects/observations/models.py +81 -0
- governed_objects/observations/store.py +214 -0
- governed_objects/observations/validation.py +188 -0
- governed_objects/outbound/__init__.py +3 -0
- governed_objects/outbound/models.py +94 -0
- governed_objects/outbound/service.py +502 -0
- governed_objects/outbound/store.py +147 -0
- governed_objects/persistence/sqlite.py +635 -0
- governed_objects/policy/constraints.py +9 -0
- governed_objects/policy/context.py +28 -0
- governed_objects/policy/evaluator.py +301 -0
- governed_objects/policy/outcomes.py +93 -0
- governed_objects/policy/reasons.py +43 -0
- governed_objects/providers/base.py +26 -0
- governed_objects/providers/binding.py +42 -0
- governed_objects/providers/capabilities.py +27 -0
- governed_objects/providers/errors.py +48 -0
- governed_objects/providers/projection.py +25 -0
- governed_objects/providers/registry.py +59 -0
- governed_objects/query/service.py +171 -0
- governed_objects/query/traversal.py +154 -0
- governed_objects/reconciliation/service.py +213 -0
- governed_objects/registration/detach.py +43 -0
- governed_objects/registration/service.py +113 -0
- governed_objects/relationships/canonical.py +55 -0
- governed_objects/relationships/sync.py +63 -0
- governed_objects/relationships/vocabulary.py +12 -0
- governed_objects/runtime/runtime.py +1919 -0
- governed_objects/types/canonicalizers/base.py +15 -0
- governed_objects/types/extensions.py +48 -0
- governed_objects/types/lifecycle.py +65 -0
- governed_objects/types/registry.py +171 -0
- governed_objects/types/schema.py +90 -0
- governed_objects/types/validation.py +57 -0
- governed_objects-0.2.1.dist-info/METADATA +184 -0
- governed_objects-0.2.1.dist-info/RECORD +88 -0
- governed_objects-0.2.1.dist-info/WHEEL +4 -0
- governed_objects-0.2.1.dist-info/licenses/LICENSE +202 -0
- governed_objects-0.2.1.dist-info/licenses/NOTICE +12 -0
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# Copyright 2026 Brafford Labs, LLC
|
|
3
|
+
"""Materialize DelegatedMandate dataclass from canonical Governed Objects."""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from datetime import datetime
|
|
8
|
+
|
|
9
|
+
from governed_objects.delegation.mandate import DelegatedMandate, MandateStatus
|
|
10
|
+
from governed_objects.delegation.scope import MandateScope, RelationshipConstraint
|
|
11
|
+
from governed_objects.domain.models import GovernedObject, Relationship
|
|
12
|
+
from governed_objects.persistence.sqlite import SQLiteStore
|
|
13
|
+
from governed_objects.relationships.canonical import list_canonical_for_object
|
|
14
|
+
from governed_objects.relationships.vocabulary import (
|
|
15
|
+
DELEGATED_BY,
|
|
16
|
+
DELEGATED_TO,
|
|
17
|
+
DERIVED_FROM,
|
|
18
|
+
SCOPED_TO,
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def materialize_mandate(
|
|
23
|
+
obj: GovernedObject,
|
|
24
|
+
store: SQLiteStore,
|
|
25
|
+
*,
|
|
26
|
+
scope_decoder=None,
|
|
27
|
+
relationships: list[Relationship] | None = None,
|
|
28
|
+
) -> DelegatedMandate:
|
|
29
|
+
"""Build mandate view from stored GO and canonical relationships."""
|
|
30
|
+
if set(obj.data) - {'capabilities', 'purposes', 'scope', 'valid_from', 'valid_until', 'parent_mandate_id'}:
|
|
31
|
+
raise ValueError('unsupported mandate data')
|
|
32
|
+
relationships = list_canonical_for_object(store, obj.id) if relationships is None else relationships
|
|
33
|
+
rel_map: dict[str, list[str]] = {}
|
|
34
|
+
for rel in relationships:
|
|
35
|
+
if rel.source_id != obj.id:
|
|
36
|
+
raise ValueError('invalid mandate relationship source')
|
|
37
|
+
rel_map.setdefault(rel.relation, []).append(rel.target_id)
|
|
38
|
+
|
|
39
|
+
principal_id = _single(rel_map.get(DELEGATED_BY, []), field="delegated_by")
|
|
40
|
+
delegate_actor_id = _single(rel_map.get(DELEGATED_TO, []), field="delegated_to")
|
|
41
|
+
parent_field = obj.data.get('parent_mandate_id')
|
|
42
|
+
if parent_field is not None and (not isinstance(parent_field, str) or not parent_field):
|
|
43
|
+
raise ValueError('invalid parent mandate')
|
|
44
|
+
parent_relation = _optional_single(rel_map.get(DERIVED_FROM, []))
|
|
45
|
+
if parent_field is not None and parent_relation is not None and parent_field != parent_relation:
|
|
46
|
+
raise ValueError('conflicting parent mandate')
|
|
47
|
+
parent_mandate_id = parent_field if parent_field is not None else parent_relation
|
|
48
|
+
|
|
49
|
+
scope = _build_scope(obj, rel_map.get(SCOPED_TO, []), store, scope_decoder)
|
|
50
|
+
|
|
51
|
+
valid_from = _parse_dt(obj.data.get("valid_from"))
|
|
52
|
+
valid_until = _parse_dt(obj.data.get("valid_until"))
|
|
53
|
+
if valid_from is not None and valid_until is not None and valid_from > valid_until:
|
|
54
|
+
raise ValueError('invalid validity interval')
|
|
55
|
+
|
|
56
|
+
status = _map_status(obj.state)
|
|
57
|
+
|
|
58
|
+
return DelegatedMandate(
|
|
59
|
+
id=obj.id,
|
|
60
|
+
principal_id=principal_id,
|
|
61
|
+
delegate_actor_id=delegate_actor_id,
|
|
62
|
+
capabilities=_strings(obj.data.get('capabilities'), required=True),
|
|
63
|
+
scope=scope,
|
|
64
|
+
purposes=_strings(obj.data.get('purposes', [])),
|
|
65
|
+
valid_from=valid_from,
|
|
66
|
+
valid_until=valid_until,
|
|
67
|
+
status=status,
|
|
68
|
+
parent_mandate_id=parent_mandate_id,
|
|
69
|
+
version=obj.version,
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def _build_scope(
|
|
74
|
+
obj: GovernedObject,
|
|
75
|
+
scoped_to: list[str],
|
|
76
|
+
store: SQLiteStore,
|
|
77
|
+
scope_decoder=None,
|
|
78
|
+
) -> MandateScope:
|
|
79
|
+
raw = obj.data.get('scope', {})
|
|
80
|
+
if not isinstance(raw, dict) or set(raw) - {'object_ids', 'object_types', 'relationship_constraints'}:
|
|
81
|
+
raise ValueError('unsupported mandate scope')
|
|
82
|
+
if raw:
|
|
83
|
+
raw_constraints = raw.get('relationship_constraints', [])
|
|
84
|
+
if not isinstance(raw_constraints, list) or any(
|
|
85
|
+
not isinstance(c, dict) or set(c) != {'relation', 'target_id'}
|
|
86
|
+
or any(not isinstance(value, str) or not value for value in c.values())
|
|
87
|
+
for c in raw_constraints
|
|
88
|
+
):
|
|
89
|
+
raise ValueError('invalid relationship constraint')
|
|
90
|
+
constraints = tuple(
|
|
91
|
+
RelationshipConstraint(relation=c["relation"], target_id=c["target_id"])
|
|
92
|
+
for c in raw_constraints
|
|
93
|
+
)
|
|
94
|
+
object_ids = _strings(raw.get('object_ids', []))
|
|
95
|
+
if scoped_to and set(scoped_to) != set(object_ids) | {c.target_id for c in constraints}:
|
|
96
|
+
raise ValueError('conflicting scope encodings')
|
|
97
|
+
return MandateScope(
|
|
98
|
+
object_ids=object_ids,
|
|
99
|
+
object_types=_strings(raw.get('object_types', [])),
|
|
100
|
+
relationship_constraints=constraints,
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
decoder = scope_decoder or reject_legacy_scope
|
|
104
|
+
scope = decoder(tuple(scoped_to), store)
|
|
105
|
+
if not isinstance(scope, MandateScope):
|
|
106
|
+
raise ValueError('unsupported scope decoder result')
|
|
107
|
+
return scope
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def reject_legacy_scope(scoped_to, store) -> MandateScope:
|
|
111
|
+
if scoped_to:
|
|
112
|
+
raise ValueError('legacy scope requires an explicit interpreter')
|
|
113
|
+
return MandateScope()
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def _map_status(state: str) -> MandateStatus:
|
|
117
|
+
if state == "draft":
|
|
118
|
+
return MandateStatus.DRAFT
|
|
119
|
+
if state == "revoked":
|
|
120
|
+
return MandateStatus.REVOKED
|
|
121
|
+
if state == 'active':
|
|
122
|
+
return MandateStatus.ACTIVE
|
|
123
|
+
raise ValueError('unsupported mandate status')
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def _parse_dt(value: str | None) -> datetime | None:
|
|
127
|
+
if value is None:
|
|
128
|
+
return None
|
|
129
|
+
if not isinstance(value, str) or not value:
|
|
130
|
+
raise ValueError('invalid mandate time')
|
|
131
|
+
result = datetime.fromisoformat(value)
|
|
132
|
+
if result.tzinfo is None or result.utcoffset() is None:
|
|
133
|
+
raise ValueError('mandate time requires timezone')
|
|
134
|
+
return result
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def _single(values: list[str], *, field: str) -> str:
|
|
138
|
+
if len(values) != 1 or not isinstance(values[0], str) or not values[0]:
|
|
139
|
+
raise ValueError(f"mandate requires one relationship: {field}")
|
|
140
|
+
return values[0]
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def _optional_single(values: list[str]) -> str | None:
|
|
144
|
+
return _single(values, field='derived_from') if values else None
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def _strings(value, *, required=False) -> frozenset[str]:
|
|
148
|
+
if not isinstance(value, list) or any(not isinstance(item, str) or not item for item in value):
|
|
149
|
+
raise ValueError('invalid mandate string list')
|
|
150
|
+
if required and not value:
|
|
151
|
+
raise ValueError('mandate capabilities required')
|
|
152
|
+
return frozenset(value)
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# Copyright 2026 Brafford Labs, LLC
|
|
3
|
+
"""Non-authoritative mandate index projection over canonical Governed Objects."""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from governed_objects.delegation.mandate import DelegatedMandate
|
|
8
|
+
from governed_objects.delegation.resolver import MANDATE_OBJECT_TYPE
|
|
9
|
+
from governed_objects.persistence.sqlite import SQLiteStore
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class MandateProjection:
|
|
13
|
+
"""Optional lookup index — not source of truth.
|
|
14
|
+
|
|
15
|
+
Canonical mandate state lives in Governed Object persistence.
|
|
16
|
+
This projection can be deleted and rebuilt without changing authorization.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
def __init__(self, store: SQLiteStore, *, resolver=None) -> None:
|
|
20
|
+
self._store = store
|
|
21
|
+
self._resolver = resolver
|
|
22
|
+
self._cache: dict[str, DelegatedMandate] = {}
|
|
23
|
+
|
|
24
|
+
def rebuild(self) -> int:
|
|
25
|
+
self._cache.clear()
|
|
26
|
+
count = 0
|
|
27
|
+
for obj in self._store.list_all():
|
|
28
|
+
if obj.type != MANDATE_OBJECT_TYPE:
|
|
29
|
+
continue
|
|
30
|
+
self.get(obj.id)
|
|
31
|
+
count += 1
|
|
32
|
+
return count
|
|
33
|
+
|
|
34
|
+
def get(self, mandate_id: str) -> DelegatedMandate | None:
|
|
35
|
+
if self._resolver is None:
|
|
36
|
+
raise ValueError('delegation configuration unavailable')
|
|
37
|
+
mandate = self._resolver.resolve(mandate_id)
|
|
38
|
+
if mandate is None:
|
|
39
|
+
return None
|
|
40
|
+
self._cache[mandate_id] = mandate
|
|
41
|
+
return mandate
|
|
42
|
+
|
|
43
|
+
def invalidate(self, mandate_id: str) -> None:
|
|
44
|
+
self._cache.pop(mandate_id, None)
|
|
45
|
+
|
|
46
|
+
def sync(self, mandate_id: str) -> None:
|
|
47
|
+
self.invalidate(mandate_id)
|
|
48
|
+
self.get(mandate_id)
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# Copyright 2026 Brafford Labs, LLC
|
|
3
|
+
"""Trusted internal mandate resolution from canonical Governed Objects."""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from dataclasses import dataclass
|
|
8
|
+
from datetime import datetime
|
|
9
|
+
|
|
10
|
+
from governed_objects.delegation.mandate import DelegatedMandate, MandateStatus
|
|
11
|
+
from governed_objects.delegation.materializer import materialize_mandate
|
|
12
|
+
from governed_objects.delegation.scope import EffectiveScope, MandateScope
|
|
13
|
+
from governed_objects.domain.clock import Clock, SystemClock
|
|
14
|
+
from governed_objects.domain.models import GovernedObject
|
|
15
|
+
from governed_objects.persistence.sqlite import SQLiteStore
|
|
16
|
+
from governed_objects.policy.reasons import DenyReason
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
MANDATE_OBJECT_TYPE = "DelegatedMandate"
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@dataclass(frozen=True)
|
|
23
|
+
class ResolvedMandateAuthority:
|
|
24
|
+
mandate_id: str
|
|
25
|
+
principal_id: str
|
|
26
|
+
delegate_actor_id: str
|
|
27
|
+
capabilities: frozenset[str]
|
|
28
|
+
scope: MandateScope | EffectiveScope
|
|
29
|
+
purposes: frozenset[str]
|
|
30
|
+
valid_from: datetime | None
|
|
31
|
+
valid_until: datetime | None
|
|
32
|
+
canonical_state: str
|
|
33
|
+
version: int
|
|
34
|
+
parent_mandate_ids: tuple[str, ...]
|
|
35
|
+
ancestor_ineffective_reason: DenyReason | None = None
|
|
36
|
+
purpose_restricted: bool = False
|
|
37
|
+
|
|
38
|
+
def is_effective_at(self, when: datetime) -> tuple[bool, DenyReason | None]:
|
|
39
|
+
if self.ancestor_ineffective_reason is not None:
|
|
40
|
+
return False, self.ancestor_ineffective_reason
|
|
41
|
+
if not self.capabilities:
|
|
42
|
+
return False, DenyReason.MANDATE_CAPABILITY_MISSING
|
|
43
|
+
if self.purpose_restricted and not self.purposes:
|
|
44
|
+
return False, DenyReason.PURPOSE_MISMATCH
|
|
45
|
+
if self.canonical_state == "revoked":
|
|
46
|
+
return False, DenyReason.MANDATE_REVOKED
|
|
47
|
+
if self.canonical_state != "active":
|
|
48
|
+
return False, DenyReason.MANDATE_NOT_YET_VALID
|
|
49
|
+
if self.valid_from is not None and when < self.valid_from:
|
|
50
|
+
return False, DenyReason.MANDATE_NOT_YET_VALID
|
|
51
|
+
if self.valid_until is not None and when > self.valid_until:
|
|
52
|
+
return False, DenyReason.MANDATE_EXPIRED
|
|
53
|
+
if self.valid_from is not None and self.valid_until is not None and self.valid_from > self.valid_until:
|
|
54
|
+
return False, DenyReason.MANDATE_NOT_YET_VALID
|
|
55
|
+
return True, None
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class MandateResolver:
|
|
59
|
+
"""Trusted runtime path for loading mandate authority.
|
|
60
|
+
|
|
61
|
+
This bypasses mandate authorization checks to avoid recursion
|
|
62
|
+
(evaluating mandate A must not require mandate A).
|
|
63
|
+
"""
|
|
64
|
+
|
|
65
|
+
# Four ancestors are supported. A fifth unresolved ancestor always refuses.
|
|
66
|
+
MAX_CHAIN_DEPTH = 4
|
|
67
|
+
|
|
68
|
+
def __init__(
|
|
69
|
+
self,
|
|
70
|
+
store: SQLiteStore,
|
|
71
|
+
*,
|
|
72
|
+
clock: Clock | None = None,
|
|
73
|
+
validate_binding=None,
|
|
74
|
+
scope_decoder=None,
|
|
75
|
+
) -> None:
|
|
76
|
+
self._store = store
|
|
77
|
+
self._clock = clock or SystemClock()
|
|
78
|
+
self._validate_binding = validate_binding
|
|
79
|
+
self._scope_decoder = scope_decoder
|
|
80
|
+
|
|
81
|
+
def get_object(self, mandate_id: str) -> GovernedObject | None:
|
|
82
|
+
if self._validate_binding is None:
|
|
83
|
+
raise ValueError('delegation configuration unavailable')
|
|
84
|
+
obj = self._store.get(mandate_id)
|
|
85
|
+
if obj is None or obj.type != MANDATE_OBJECT_TYPE:
|
|
86
|
+
return None
|
|
87
|
+
self._validate_binding(obj)
|
|
88
|
+
return obj
|
|
89
|
+
|
|
90
|
+
def resolve(self, mandate_id: str) -> DelegatedMandate | None:
|
|
91
|
+
obj = self.get_object(mandate_id)
|
|
92
|
+
if obj is None:
|
|
93
|
+
return None
|
|
94
|
+
return materialize_mandate(obj, self._store, scope_decoder=self._scope_decoder)
|
|
95
|
+
|
|
96
|
+
def resolve_authority(self, mandate_id: str) -> ResolvedMandateAuthority | None:
|
|
97
|
+
obj = self.get_object(mandate_id)
|
|
98
|
+
if obj is None:
|
|
99
|
+
return None
|
|
100
|
+
mandate = materialize_mandate(obj, self._store, scope_decoder=self._scope_decoder)
|
|
101
|
+
parent_ids: list[str] = []
|
|
102
|
+
ancestor_reason: DenyReason | None = None
|
|
103
|
+
current_parent = mandate.parent_mandate_id
|
|
104
|
+
visited: set[str] = {mandate_id}
|
|
105
|
+
bounds = [mandate]
|
|
106
|
+
depth = 0
|
|
107
|
+
while current_parent:
|
|
108
|
+
if depth >= self.MAX_CHAIN_DEPTH:
|
|
109
|
+
ancestor_reason = DenyReason.DELEGATION_DEPTH_EXCEEDED
|
|
110
|
+
break
|
|
111
|
+
if current_parent in visited:
|
|
112
|
+
ancestor_reason = DenyReason.DELEGATION_CYCLE
|
|
113
|
+
break
|
|
114
|
+
visited.add(current_parent)
|
|
115
|
+
parent_ids.append(current_parent)
|
|
116
|
+
parent_obj = self.get_object(current_parent)
|
|
117
|
+
if parent_obj is None:
|
|
118
|
+
ancestor_reason = DenyReason.MANDATE_UNKNOWN
|
|
119
|
+
break
|
|
120
|
+
parent = materialize_mandate(parent_obj, self._store, scope_decoder=self._scope_decoder)
|
|
121
|
+
bounds.append(parent)
|
|
122
|
+
if parent.status == MandateStatus.REVOKED or parent_obj.state == "revoked":
|
|
123
|
+
ancestor_reason = DenyReason.PARENT_MANDATE_REVOKED
|
|
124
|
+
break
|
|
125
|
+
now = self._clock.now()
|
|
126
|
+
if parent.valid_from is not None and now < parent.valid_from:
|
|
127
|
+
ancestor_reason = DenyReason.MANDATE_NOT_YET_VALID
|
|
128
|
+
break
|
|
129
|
+
if parent.valid_until is not None and now > parent.valid_until:
|
|
130
|
+
ancestor_reason = DenyReason.PARENT_MANDATE_EXPIRED
|
|
131
|
+
break
|
|
132
|
+
if parent_obj.state != "active":
|
|
133
|
+
ancestor_reason = DenyReason.PARENT_MANDATE_REVOKED
|
|
134
|
+
break
|
|
135
|
+
current_parent = parent.parent_mandate_id
|
|
136
|
+
depth += 1
|
|
137
|
+
|
|
138
|
+
effective_caps = mandate.capabilities
|
|
139
|
+
effective_scope = EffectiveScope(tuple(bound.scope for bound in bounds))
|
|
140
|
+
purpose_bounds = [bound.purposes for bound in bounds if bound.purposes]
|
|
141
|
+
effective_purposes = frozenset.intersection(*purpose_bounds) if purpose_bounds else frozenset()
|
|
142
|
+
effective_from = mandate.valid_from
|
|
143
|
+
effective_until = mandate.valid_until
|
|
144
|
+
|
|
145
|
+
for parent in bounds[1:]:
|
|
146
|
+
effective_caps = effective_caps & parent.capabilities
|
|
147
|
+
effective_from = _max_dt(effective_from, parent.valid_from)
|
|
148
|
+
effective_until = _min_dt(effective_until, parent.valid_until)
|
|
149
|
+
|
|
150
|
+
return ResolvedMandateAuthority(
|
|
151
|
+
mandate_id=mandate.id,
|
|
152
|
+
principal_id=mandate.principal_id,
|
|
153
|
+
delegate_actor_id=mandate.delegate_actor_id,
|
|
154
|
+
capabilities=effective_caps,
|
|
155
|
+
scope=effective_scope,
|
|
156
|
+
purposes=effective_purposes,
|
|
157
|
+
valid_from=effective_from,
|
|
158
|
+
valid_until=effective_until,
|
|
159
|
+
canonical_state=obj.state,
|
|
160
|
+
version=obj.version,
|
|
161
|
+
parent_mandate_ids=tuple(parent_ids),
|
|
162
|
+
ancestor_ineffective_reason=ancestor_reason,
|
|
163
|
+
purpose_restricted=bool(purpose_bounds),
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def _max_dt(a: datetime | None, b: datetime | None) -> datetime | None:
|
|
168
|
+
if a is None:
|
|
169
|
+
return b
|
|
170
|
+
if b is None:
|
|
171
|
+
return a
|
|
172
|
+
return max(a, b)
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def _min_dt(a: datetime | None, b: datetime | None) -> datetime | None:
|
|
176
|
+
if a is None:
|
|
177
|
+
return b
|
|
178
|
+
if b is None:
|
|
179
|
+
return a
|
|
180
|
+
return min(a, b)
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# Copyright 2026 Brafford Labs, LLC
|
|
3
|
+
"""Generic mandate scope evaluation."""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from dataclasses import dataclass
|
|
8
|
+
|
|
9
|
+
from governed_objects.domain.models import GovernedObject
|
|
10
|
+
from governed_objects.persistence.sqlite import SQLiteStore
|
|
11
|
+
from governed_objects.relationships.canonical import list_canonical_for_object
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@dataclass(frozen=True)
|
|
15
|
+
class RelationshipConstraint:
|
|
16
|
+
relation: str
|
|
17
|
+
target_id: str
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass(frozen=True)
|
|
21
|
+
class MandateScope:
|
|
22
|
+
object_ids: frozenset[str] = frozenset()
|
|
23
|
+
object_types: frozenset[str] = frozenset()
|
|
24
|
+
relationship_constraints: tuple[RelationshipConstraint, ...] = ()
|
|
25
|
+
|
|
26
|
+
def __post_init__(self):
|
|
27
|
+
for values in (self.object_ids, self.object_types):
|
|
28
|
+
if not isinstance(values, frozenset) or any(not isinstance(value, str) or not value for value in values):
|
|
29
|
+
raise ValueError('invalid mandate scope')
|
|
30
|
+
if not isinstance(self.relationship_constraints, tuple) or any(
|
|
31
|
+
not isinstance(value, RelationshipConstraint)
|
|
32
|
+
or not isinstance(value.relation, str) or not value.relation
|
|
33
|
+
or not isinstance(value.target_id, str) or not value.target_id
|
|
34
|
+
for value in self.relationship_constraints
|
|
35
|
+
):
|
|
36
|
+
raise ValueError('invalid mandate scope')
|
|
37
|
+
|
|
38
|
+
def is_empty(self) -> bool:
|
|
39
|
+
return (
|
|
40
|
+
not self.object_ids
|
|
41
|
+
and not self.object_types
|
|
42
|
+
and not self.relationship_constraints
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
@dataclass(frozen=True)
|
|
47
|
+
class EffectiveScope:
|
|
48
|
+
"""Conjunction of declarations, never a flattened declaration or saved data."""
|
|
49
|
+
|
|
50
|
+
bounds: tuple[MandateScope, ...]
|
|
51
|
+
|
|
52
|
+
def is_empty(self) -> bool:
|
|
53
|
+
return all(bound.is_empty() for bound in self.bounds)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def scope_allows(
|
|
57
|
+
scope: MandateScope | EffectiveScope,
|
|
58
|
+
obj: GovernedObject,
|
|
59
|
+
store: SQLiteStore,
|
|
60
|
+
) -> bool:
|
|
61
|
+
if isinstance(scope, EffectiveScope):
|
|
62
|
+
return all(scope_allows(bound, obj, store) for bound in scope.bounds)
|
|
63
|
+
if scope.is_empty():
|
|
64
|
+
return True
|
|
65
|
+
if obj.id in scope.object_ids:
|
|
66
|
+
return True
|
|
67
|
+
if obj.type in scope.object_types:
|
|
68
|
+
return True
|
|
69
|
+
if scope.relationship_constraints:
|
|
70
|
+
relationships = list_canonical_for_object(store, obj.id)
|
|
71
|
+
edges = {(r.relation, r.target_id) for r in relationships if r.source_id == obj.id}
|
|
72
|
+
for constraint in scope.relationship_constraints:
|
|
73
|
+
if (constraint.relation, constraint.target_id) in edges:
|
|
74
|
+
return True
|
|
75
|
+
return False
|