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,245 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# Copyright 2026 Brafford Labs, LLC
|
|
3
|
+
"""Mandate creation, activation, and revocation via Governed Object runtime."""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from datetime import datetime
|
|
8
|
+
from typing import TYPE_CHECKING
|
|
9
|
+
|
|
10
|
+
from governed_objects.delegation.mandate import DelegatedMandate, MandateStatus
|
|
11
|
+
from governed_objects.delegation.resolver import MandateResolver
|
|
12
|
+
from governed_objects.delegation.scope import MandateScope
|
|
13
|
+
from governed_objects.delegation.validator import (
|
|
14
|
+
DelegationSpec,
|
|
15
|
+
DelegationValidator,
|
|
16
|
+
DelegationViolation,
|
|
17
|
+
)
|
|
18
|
+
from governed_objects.domain.actors import ActorRegistry
|
|
19
|
+
from governed_objects.domain.command import CreateObject, TransitionState
|
|
20
|
+
from governed_objects.domain.models import Actor, Decision
|
|
21
|
+
from governed_objects.relationships.vocabulary import (
|
|
22
|
+
DELEGATED_BY,
|
|
23
|
+
DELEGATED_TO,
|
|
24
|
+
DERIVED_FROM,
|
|
25
|
+
SCOPED_TO,
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
if TYPE_CHECKING:
|
|
29
|
+
from governed_objects.runtime.runtime import GovernedObjectRuntime
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class MandateValidationError(ValueError):
|
|
33
|
+
def __init__(self, message: str, violations: list[DelegationViolation] | None = None) -> None:
|
|
34
|
+
super().__init__(message)
|
|
35
|
+
self.violations = violations or []
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class MandateService:
|
|
39
|
+
"""Manages DelegatedMandate Governed Objects."""
|
|
40
|
+
|
|
41
|
+
def __init__(
|
|
42
|
+
self,
|
|
43
|
+
runtime: GovernedObjectRuntime,
|
|
44
|
+
resolver: MandateResolver,
|
|
45
|
+
validator: DelegationValidator,
|
|
46
|
+
actors: ActorRegistry | None = None,
|
|
47
|
+
) -> None:
|
|
48
|
+
self._runtime = runtime
|
|
49
|
+
self._resolver = resolver
|
|
50
|
+
self._validator = validator
|
|
51
|
+
self._actors = actors
|
|
52
|
+
|
|
53
|
+
def create_mandate(
|
|
54
|
+
self,
|
|
55
|
+
*,
|
|
56
|
+
mandate_id: str,
|
|
57
|
+
principal: Actor,
|
|
58
|
+
delegate_actor_id: str,
|
|
59
|
+
capabilities: frozenset[str],
|
|
60
|
+
scope: MandateScope | None = None,
|
|
61
|
+
purposes: frozenset[str] | None = None,
|
|
62
|
+
valid_from: datetime | None = None,
|
|
63
|
+
valid_until: datetime | None = None,
|
|
64
|
+
parent_mandate_id: str | None = None,
|
|
65
|
+
auto_activate: bool = True,
|
|
66
|
+
) -> DelegatedMandate:
|
|
67
|
+
if (not isinstance(capabilities, frozenset) or not capabilities
|
|
68
|
+
or any(not isinstance(cap, str) or not cap for cap in capabilities)
|
|
69
|
+
or (scope is not None and not isinstance(scope, MandateScope))
|
|
70
|
+
or (purposes is not None and (not isinstance(purposes, frozenset)
|
|
71
|
+
or any(not isinstance(purpose, str) or not purpose for purpose in purposes)))):
|
|
72
|
+
raise MandateValidationError('invalid delegation input')
|
|
73
|
+
if any(value is not None and (not isinstance(value, datetime) or value.tzinfo is None
|
|
74
|
+
or value.utcoffset() is None) for value in (valid_from, valid_until)):
|
|
75
|
+
raise MandateValidationError('invalid delegation time')
|
|
76
|
+
try:
|
|
77
|
+
configuration = self._runtime.require_delegation()
|
|
78
|
+
configuration.actor(principal.id)
|
|
79
|
+
configuration.actor(delegate_actor_id)
|
|
80
|
+
except ValueError:
|
|
81
|
+
raise MandateValidationError('delegation configuration unavailable') from None
|
|
82
|
+
parent = None
|
|
83
|
+
ancestor_ids: frozenset[str] = frozenset()
|
|
84
|
+
if parent_mandate_id:
|
|
85
|
+
if mandate_id == parent_mandate_id:
|
|
86
|
+
raise MandateValidationError(
|
|
87
|
+
"delegation validation failed: DELEGATION_CYCLE",
|
|
88
|
+
violations=[DelegationViolation.DELEGATION_CYCLE],
|
|
89
|
+
)
|
|
90
|
+
parent = self._resolver.resolve(parent_mandate_id)
|
|
91
|
+
if parent is None:
|
|
92
|
+
raise MandateValidationError(f"unknown parent mandate: {parent_mandate_id}")
|
|
93
|
+
resolved = self._resolver.resolve_authority(parent_mandate_id)
|
|
94
|
+
if resolved is not None:
|
|
95
|
+
ancestor_ids = frozenset(resolved.parent_mandate_ids)
|
|
96
|
+
|
|
97
|
+
spec = DelegationSpec(
|
|
98
|
+
capabilities=capabilities,
|
|
99
|
+
scope=scope or MandateScope(),
|
|
100
|
+
purposes=purposes or frozenset(),
|
|
101
|
+
valid_from=valid_from,
|
|
102
|
+
valid_until=valid_until,
|
|
103
|
+
parent_mandate_id=parent_mandate_id,
|
|
104
|
+
mandate_id=mandate_id,
|
|
105
|
+
)
|
|
106
|
+
result = self._validator.validate(
|
|
107
|
+
principal=principal,
|
|
108
|
+
spec=spec,
|
|
109
|
+
parent_mandate=parent,
|
|
110
|
+
ancestor_ids=ancestor_ids,
|
|
111
|
+
)
|
|
112
|
+
if not result.valid:
|
|
113
|
+
raise MandateValidationError(
|
|
114
|
+
f"delegation validation failed: {', '.join(v.value for v in result.violations)}",
|
|
115
|
+
violations=result.violations,
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
if self._actors is not None:
|
|
119
|
+
self._actors.resolve(delegate_actor_id)
|
|
120
|
+
|
|
121
|
+
scope = scope or MandateScope()
|
|
122
|
+
relationships = [
|
|
123
|
+
{"source_id": mandate_id, "relation": DELEGATED_BY, "target_id": principal.id},
|
|
124
|
+
{"source_id": mandate_id, "relation": DELEGATED_TO, "target_id": delegate_actor_id},
|
|
125
|
+
]
|
|
126
|
+
if parent_mandate_id:
|
|
127
|
+
relationships.append(
|
|
128
|
+
{
|
|
129
|
+
"source_id": mandate_id,
|
|
130
|
+
"relation": DERIVED_FROM,
|
|
131
|
+
"target_id": parent_mandate_id,
|
|
132
|
+
}
|
|
133
|
+
)
|
|
134
|
+
for target_id in _scoped_targets(scope):
|
|
135
|
+
relationships.append(
|
|
136
|
+
{"source_id": mandate_id, "relation": SCOPED_TO, "target_id": target_id}
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
data = {
|
|
140
|
+
"capabilities": sorted(capabilities),
|
|
141
|
+
"purposes": sorted(purposes or []),
|
|
142
|
+
"valid_from": valid_from.isoformat() if valid_from else None,
|
|
143
|
+
"valid_until": valid_until.isoformat() if valid_until else None,
|
|
144
|
+
"parent_mandate_id": parent_mandate_id,
|
|
145
|
+
"scope": _scope_to_data(scope),
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
create = self._runtime.execute(
|
|
149
|
+
CreateObject(
|
|
150
|
+
mandate_id,
|
|
151
|
+
"DelegatedMandate",
|
|
152
|
+
data,
|
|
153
|
+
state="draft",
|
|
154
|
+
relationships=relationships,
|
|
155
|
+
),
|
|
156
|
+
actor=principal,
|
|
157
|
+
)
|
|
158
|
+
if create.decision != Decision.ALLOW:
|
|
159
|
+
raise MandateValidationError(create.reason or "mandate create denied")
|
|
160
|
+
|
|
161
|
+
self._runtime.mandate_projection.sync(mandate_id)
|
|
162
|
+
|
|
163
|
+
if auto_activate:
|
|
164
|
+
activated = self.activate(mandate_id, actor=principal)
|
|
165
|
+
if activated is None:
|
|
166
|
+
raise MandateValidationError("mandate activation failed")
|
|
167
|
+
return activated
|
|
168
|
+
|
|
169
|
+
mandate = self.get(mandate_id)
|
|
170
|
+
if mandate is None:
|
|
171
|
+
raise MandateValidationError("mandate not found after create")
|
|
172
|
+
return mandate
|
|
173
|
+
|
|
174
|
+
def activate(
|
|
175
|
+
self,
|
|
176
|
+
mandate_id: str,
|
|
177
|
+
*,
|
|
178
|
+
actor: Actor,
|
|
179
|
+
expected_version: int | None = None,
|
|
180
|
+
) -> DelegatedMandate | None:
|
|
181
|
+
obj = self._resolver.get_object(mandate_id)
|
|
182
|
+
if obj is None:
|
|
183
|
+
return None
|
|
184
|
+
version = expected_version if expected_version is not None else obj.version
|
|
185
|
+
result = self._runtime.execute(
|
|
186
|
+
TransitionState(mandate_id, "activate"),
|
|
187
|
+
actor=actor,
|
|
188
|
+
command_context=self._runtime._make_context(expected_version=version),
|
|
189
|
+
)
|
|
190
|
+
if result.decision != Decision.ALLOW:
|
|
191
|
+
return None
|
|
192
|
+
self._runtime.mandate_projection.sync(mandate_id)
|
|
193
|
+
return self.get(mandate_id)
|
|
194
|
+
|
|
195
|
+
def revoke(
|
|
196
|
+
self,
|
|
197
|
+
mandate_id: str,
|
|
198
|
+
*,
|
|
199
|
+
actor: Actor | None = None,
|
|
200
|
+
expected_version: int | None = None,
|
|
201
|
+
) -> DelegatedMandate | None:
|
|
202
|
+
mandate = self.get(mandate_id)
|
|
203
|
+
if mandate is None:
|
|
204
|
+
return None
|
|
205
|
+
if actor is None:
|
|
206
|
+
if self._actors is None:
|
|
207
|
+
return None
|
|
208
|
+
actor = self._actors.resolve(mandate.principal_id)
|
|
209
|
+
obj = self._resolver.get_object(mandate_id)
|
|
210
|
+
if obj is None:
|
|
211
|
+
return None
|
|
212
|
+
version = expected_version if expected_version is not None else obj.version
|
|
213
|
+
result = self._runtime.execute(
|
|
214
|
+
TransitionState(mandate_id, "revoke"),
|
|
215
|
+
actor=actor,
|
|
216
|
+
command_context=self._runtime._make_context(expected_version=version),
|
|
217
|
+
)
|
|
218
|
+
if result.decision != Decision.ALLOW:
|
|
219
|
+
return None
|
|
220
|
+
self._runtime.mandate_projection.sync(mandate_id)
|
|
221
|
+
revoked = self.get(mandate_id)
|
|
222
|
+
if revoked is not None:
|
|
223
|
+
revoked.status = MandateStatus.REVOKED
|
|
224
|
+
return revoked
|
|
225
|
+
|
|
226
|
+
def get(self, mandate_id: str) -> DelegatedMandate | None:
|
|
227
|
+
return self._resolver.resolve(mandate_id)
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def _scoped_targets(scope: MandateScope) -> list[str]:
|
|
231
|
+
targets = list(scope.object_ids)
|
|
232
|
+
for constraint in scope.relationship_constraints:
|
|
233
|
+
targets.append(constraint.target_id)
|
|
234
|
+
return sorted(set(targets))
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
def _scope_to_data(scope: MandateScope) -> dict:
|
|
238
|
+
return {
|
|
239
|
+
"object_ids": sorted(scope.object_ids),
|
|
240
|
+
"object_types": sorted(scope.object_types),
|
|
241
|
+
"relationship_constraints": [
|
|
242
|
+
{"relation": c.relation, "target_id": c.target_id}
|
|
243
|
+
for c in scope.relationship_constraints
|
|
244
|
+
],
|
|
245
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# Copyright 2026 Brafford Labs, LLC
|
|
3
|
+
"""Generic monotonic delegation validation."""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from dataclasses import dataclass, field
|
|
8
|
+
from datetime import datetime
|
|
9
|
+
from enum import StrEnum
|
|
10
|
+
|
|
11
|
+
from governed_objects.delegation.mandate import DelegatedMandate
|
|
12
|
+
from governed_objects.delegation.scope import EffectiveScope, MandateScope, scope_allows
|
|
13
|
+
from governed_objects.domain.models import Actor
|
|
14
|
+
from governed_objects.persistence.sqlite import SQLiteStore
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class DelegationViolation(StrEnum):
|
|
18
|
+
CAPABILITY_EXPANSION = "CAPABILITY_EXPANSION"
|
|
19
|
+
SCOPE_EXPANSION = "SCOPE_EXPANSION"
|
|
20
|
+
PURPOSE_EXPANSION = "PURPOSE_EXPANSION"
|
|
21
|
+
TIME_EXPANSION = "TIME_EXPANSION"
|
|
22
|
+
DELEGATION_CYCLE = "DELEGATION_CYCLE"
|
|
23
|
+
DELEGATION_DEPTH_EXCEEDED = "DELEGATION_DEPTH_EXCEEDED"
|
|
24
|
+
PRINCIPAL_CAPABILITY_MISSING = "PRINCIPAL_CAPABILITY_MISSING"
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@dataclass
|
|
28
|
+
class DelegationValidationResult:
|
|
29
|
+
valid: bool
|
|
30
|
+
violations: list[DelegationViolation] = field(default_factory=list)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@dataclass(frozen=True)
|
|
34
|
+
class DelegationSpec:
|
|
35
|
+
capabilities: frozenset[str]
|
|
36
|
+
scope: MandateScope
|
|
37
|
+
purposes: frozenset[str]
|
|
38
|
+
valid_from: datetime | None
|
|
39
|
+
valid_until: datetime | None
|
|
40
|
+
parent_mandate_id: str | None = None
|
|
41
|
+
mandate_id: str | None = None
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@dataclass(frozen=True)
|
|
45
|
+
class DelegationAuthority:
|
|
46
|
+
capabilities: frozenset[str]
|
|
47
|
+
scope: MandateScope
|
|
48
|
+
purposes: frozenset[str]
|
|
49
|
+
valid_from: datetime | None
|
|
50
|
+
valid_until: datetime | None
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class DelegationValidator:
|
|
54
|
+
MAX_DELEGATION_DEPTH = 2
|
|
55
|
+
|
|
56
|
+
def __init__(self, store: SQLiteStore) -> None:
|
|
57
|
+
self._store = store
|
|
58
|
+
|
|
59
|
+
def validate(
|
|
60
|
+
self,
|
|
61
|
+
*,
|
|
62
|
+
principal: Actor,
|
|
63
|
+
spec: DelegationSpec,
|
|
64
|
+
parent_mandate: DelegatedMandate | None = None,
|
|
65
|
+
ancestor_ids: frozenset[str] | None = None,
|
|
66
|
+
max_depth: int | None = None,
|
|
67
|
+
) -> DelegationValidationResult:
|
|
68
|
+
violations: list[DelegationViolation] = []
|
|
69
|
+
|
|
70
|
+
if spec.mandate_id and spec.parent_mandate_id:
|
|
71
|
+
if spec.mandate_id == spec.parent_mandate_id:
|
|
72
|
+
violations.append(DelegationViolation.DELEGATION_CYCLE)
|
|
73
|
+
|
|
74
|
+
limit = self.MAX_DELEGATION_DEPTH if max_depth is None else max_depth
|
|
75
|
+
if parent_mandate is not None and len(ancestor_ids or ()) + 2 > limit:
|
|
76
|
+
violations.append(DelegationViolation.DELEGATION_DEPTH_EXCEEDED)
|
|
77
|
+
|
|
78
|
+
if parent_mandate is None:
|
|
79
|
+
ceiling = DelegationAuthority(
|
|
80
|
+
capabilities=frozenset(principal.capabilities),
|
|
81
|
+
scope=MandateScope(),
|
|
82
|
+
purposes=frozenset(),
|
|
83
|
+
valid_from=None,
|
|
84
|
+
valid_until=None,
|
|
85
|
+
)
|
|
86
|
+
if not spec.capabilities.issubset(ceiling.capabilities):
|
|
87
|
+
violations.append(DelegationViolation.PRINCIPAL_CAPABILITY_MISSING)
|
|
88
|
+
else:
|
|
89
|
+
ceiling = DelegationAuthority(
|
|
90
|
+
capabilities=parent_mandate.capabilities,
|
|
91
|
+
scope=parent_mandate.scope,
|
|
92
|
+
purposes=parent_mandate.purposes,
|
|
93
|
+
valid_from=parent_mandate.valid_from,
|
|
94
|
+
valid_until=parent_mandate.valid_until,
|
|
95
|
+
)
|
|
96
|
+
principal_caps = set(principal.capabilities)
|
|
97
|
+
if not spec.capabilities.issubset(principal_caps):
|
|
98
|
+
violations.append(DelegationViolation.PRINCIPAL_CAPABILITY_MISSING)
|
|
99
|
+
|
|
100
|
+
if not spec.capabilities.issubset(ceiling.capabilities):
|
|
101
|
+
violations.append(DelegationViolation.CAPABILITY_EXPANSION)
|
|
102
|
+
|
|
103
|
+
if not _scope_subset(spec.scope, ceiling.scope, self._store):
|
|
104
|
+
violations.append(DelegationViolation.SCOPE_EXPANSION)
|
|
105
|
+
|
|
106
|
+
if ceiling.purposes:
|
|
107
|
+
if spec.purposes and not spec.purposes.issubset(ceiling.purposes):
|
|
108
|
+
violations.append(DelegationViolation.PURPOSE_EXPANSION)
|
|
109
|
+
elif spec.purposes and parent_mandate is not None:
|
|
110
|
+
pass
|
|
111
|
+
|
|
112
|
+
if not _time_subset(
|
|
113
|
+
spec.valid_from,
|
|
114
|
+
spec.valid_until,
|
|
115
|
+
ceiling.valid_from,
|
|
116
|
+
ceiling.valid_until,
|
|
117
|
+
):
|
|
118
|
+
violations.append(DelegationViolation.TIME_EXPANSION)
|
|
119
|
+
|
|
120
|
+
return DelegationValidationResult(valid=not violations, violations=violations)
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def _time_subset(
|
|
124
|
+
child_from: datetime | None,
|
|
125
|
+
child_until: datetime | None,
|
|
126
|
+
parent_from: datetime | None,
|
|
127
|
+
parent_until: datetime | None,
|
|
128
|
+
) -> bool:
|
|
129
|
+
if parent_from is not None and child_from is not None and child_from < parent_from:
|
|
130
|
+
return False
|
|
131
|
+
if parent_until is not None and child_until is not None and child_until > parent_until:
|
|
132
|
+
return False
|
|
133
|
+
return True
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def _scope_subset(child: MandateScope, parent: MandateScope, store: SQLiteStore) -> bool:
|
|
137
|
+
if isinstance(parent, EffectiveScope):
|
|
138
|
+
return all(_scope_subset(child, bound, store) for bound in parent.bounds)
|
|
139
|
+
if parent.is_empty():
|
|
140
|
+
return True
|
|
141
|
+
if child.is_empty():
|
|
142
|
+
return True
|
|
143
|
+
|
|
144
|
+
for oid in child.object_ids:
|
|
145
|
+
obj = store.get(oid)
|
|
146
|
+
if obj is None:
|
|
147
|
+
return False
|
|
148
|
+
if not scope_allows(parent, obj, store):
|
|
149
|
+
return False
|
|
150
|
+
|
|
151
|
+
for ctype in child.object_types:
|
|
152
|
+
if ctype not in parent.object_types:
|
|
153
|
+
return False
|
|
154
|
+
|
|
155
|
+
for constraint in child.relationship_constraints:
|
|
156
|
+
if constraint not in parent.relationship_constraints:
|
|
157
|
+
return False
|
|
158
|
+
|
|
159
|
+
return True
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# Copyright 2026 Brafford Labs, LLC
|
|
3
|
+
from governed_objects.domain.models import (
|
|
4
|
+
Actor,
|
|
5
|
+
CommandResult,
|
|
6
|
+
Decision,
|
|
7
|
+
GovernedObject,
|
|
8
|
+
GovernanceEvent,
|
|
9
|
+
Relationship,
|
|
10
|
+
ValidationCheck,
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
__all__ = [
|
|
14
|
+
"Actor",
|
|
15
|
+
"CommandResult",
|
|
16
|
+
"Decision",
|
|
17
|
+
"GovernedObject",
|
|
18
|
+
"GovernanceEvent",
|
|
19
|
+
"Relationship",
|
|
20
|
+
"ValidationCheck",
|
|
21
|
+
]
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# Copyright 2026 Brafford Labs, LLC
|
|
3
|
+
"""Versioned actor manifest model, used to generate the published actor JSON Schema.
|
|
4
|
+
|
|
5
|
+
This is a description of the manifest format only. ``ActorRegistry`` remains the
|
|
6
|
+
loader and does not depend on this module.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from typing import Literal
|
|
12
|
+
|
|
13
|
+
from pydantic import BaseModel, Field
|
|
14
|
+
|
|
15
|
+
from governed_objects.domain.models import ActorType
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class ActorMetadata(BaseModel):
|
|
19
|
+
"""Identity metadata for an actor manifest."""
|
|
20
|
+
|
|
21
|
+
id: str
|
|
22
|
+
name: str | None = None
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class ActorSpecBody(BaseModel):
|
|
26
|
+
"""Declarative actor capabilities."""
|
|
27
|
+
|
|
28
|
+
type: ActorType
|
|
29
|
+
capabilities: list[str] = Field(default_factory=list)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class ActorResource(BaseModel):
|
|
33
|
+
"""Versioned actor manifest."""
|
|
34
|
+
|
|
35
|
+
api_version: str = Field(alias="apiVersion")
|
|
36
|
+
kind: Literal["Actor"]
|
|
37
|
+
metadata: ActorMetadata
|
|
38
|
+
spec: ActorSpecBody
|
|
39
|
+
|
|
40
|
+
model_config = {"populate_by_name": True}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# Copyright 2026 Brafford Labs, LLC
|
|
3
|
+
"""Actor loading for the governed object runtime."""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
import yaml
|
|
10
|
+
|
|
11
|
+
from governed_objects.domain.models import Actor, ActorType
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class ActorRegistry:
|
|
15
|
+
"""Resolve actors from YAML manifests (governed_objects format; legacy apiVersion labels are accepted and ignored)."""
|
|
16
|
+
|
|
17
|
+
def __init__(self, actors_dir: Path) -> None:
|
|
18
|
+
self._actors_dir = actors_dir
|
|
19
|
+
self._cache: dict[str, Actor] = {}
|
|
20
|
+
|
|
21
|
+
def resolve(self, actor_id: str) -> Actor:
|
|
22
|
+
if actor_id in self._cache:
|
|
23
|
+
return self._cache[actor_id]
|
|
24
|
+
|
|
25
|
+
actor_path = self._actors_dir / f"{actor_id}.yaml"
|
|
26
|
+
if not actor_path.exists():
|
|
27
|
+
raise ValueError(f"Unknown actor: {actor_id}")
|
|
28
|
+
|
|
29
|
+
raw = yaml.safe_load(actor_path.read_text()) or {}
|
|
30
|
+
actor = self._parse_actor(raw, actor_id)
|
|
31
|
+
self._cache[actor_id] = actor
|
|
32
|
+
return actor
|
|
33
|
+
|
|
34
|
+
def list_actors(self) -> list[str]:
|
|
35
|
+
return sorted(p.stem for p in self._actors_dir.glob("*.yaml"))
|
|
36
|
+
|
|
37
|
+
@staticmethod
|
|
38
|
+
def _parse_actor(raw: dict, actor_id: str) -> Actor:
|
|
39
|
+
if raw.get("kind") == "Actor" and "spec" in raw:
|
|
40
|
+
spec = raw["spec"]
|
|
41
|
+
meta = raw.get("metadata") or {}
|
|
42
|
+
return Actor(
|
|
43
|
+
id=meta.get("id", actor_id),
|
|
44
|
+
type=ActorType(spec.get("type", "human")),
|
|
45
|
+
capabilities=spec.get("capabilities") or [],
|
|
46
|
+
constraints=spec.get("constraints") or [],
|
|
47
|
+
display_name=meta.get("name"),
|
|
48
|
+
)
|
|
49
|
+
return Actor(
|
|
50
|
+
id=raw.get("id", actor_id),
|
|
51
|
+
type=ActorType(raw.get("type", "human")),
|
|
52
|
+
capabilities=raw.get("capabilities") or [],
|
|
53
|
+
constraints=raw.get("constraints") or [],
|
|
54
|
+
display_name=raw.get("name"),
|
|
55
|
+
)
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# Copyright 2026 Brafford Labs, LLC
|
|
3
|
+
"""Injectable clock for deterministic policy and agent tests."""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from datetime import UTC, datetime
|
|
8
|
+
from typing import Protocol
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class Clock(Protocol):
|
|
12
|
+
def now(self) -> datetime: ...
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class SystemClock:
|
|
16
|
+
def now(self) -> datetime:
|
|
17
|
+
return datetime.now(UTC)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class FixedClock:
|
|
21
|
+
def __init__(self, fixed: datetime) -> None:
|
|
22
|
+
self._fixed = fixed
|
|
23
|
+
|
|
24
|
+
def now(self) -> datetime:
|
|
25
|
+
return self._fixed
|
|
26
|
+
|
|
27
|
+
def advance_to(self, when: datetime) -> None:
|
|
28
|
+
self._fixed = when
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# Copyright 2026 Brafford Labs, LLC
|
|
3
|
+
"""Command definitions for the governed object runtime."""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from dataclasses import dataclass, field
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@dataclass(frozen=True)
|
|
13
|
+
class Command:
|
|
14
|
+
action: str
|
|
15
|
+
object_id: str | None = None
|
|
16
|
+
object_type: str | None = None
|
|
17
|
+
payload: dict[str, Any] = field(default_factory=dict)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass(frozen=True)
|
|
21
|
+
class CreateObject(Command):
|
|
22
|
+
def __init__(
|
|
23
|
+
self,
|
|
24
|
+
object_id: str,
|
|
25
|
+
object_type: str,
|
|
26
|
+
data: dict[str, Any],
|
|
27
|
+
*,
|
|
28
|
+
state: str | None = None,
|
|
29
|
+
metadata: dict[str, Any] | None = None,
|
|
30
|
+
relationships: list[dict[str, str]] | None = None,
|
|
31
|
+
) -> None:
|
|
32
|
+
object.__setattr__(self, "action", "create")
|
|
33
|
+
object.__setattr__(self, "object_id", object_id)
|
|
34
|
+
object.__setattr__(self, "object_type", object_type)
|
|
35
|
+
object.__setattr__(
|
|
36
|
+
self,
|
|
37
|
+
"payload",
|
|
38
|
+
{
|
|
39
|
+
"data": data,
|
|
40
|
+
"state": state,
|
|
41
|
+
"metadata": metadata or {},
|
|
42
|
+
"relationships": relationships or [],
|
|
43
|
+
},
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
@dataclass(frozen=True)
|
|
48
|
+
class TransitionState(Command):
|
|
49
|
+
def __init__(self, object_id: str, transition: str) -> None:
|
|
50
|
+
object.__setattr__(self, "action", transition)
|
|
51
|
+
object.__setattr__(self, "object_id", object_id)
|
|
52
|
+
object.__setattr__(self, "object_type", None)
|
|
53
|
+
object.__setattr__(self, "payload", {})
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@dataclass(frozen=True)
|
|
57
|
+
class ReadObject(Command):
|
|
58
|
+
def __init__(self, object_id: str, fields: list[str] | None = None) -> None:
|
|
59
|
+
object.__setattr__(self, "action", "read")
|
|
60
|
+
object.__setattr__(self, "object_id", object_id)
|
|
61
|
+
object.__setattr__(self, "object_type", None)
|
|
62
|
+
object.__setattr__(self, "payload", {"fields": fields or []})
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
@dataclass(frozen=True)
|
|
66
|
+
class AddRelationship(Command):
|
|
67
|
+
def __init__(
|
|
68
|
+
self,
|
|
69
|
+
object_id: str,
|
|
70
|
+
relation: str,
|
|
71
|
+
target_id: str,
|
|
72
|
+
*,
|
|
73
|
+
target_type: str | None = None,
|
|
74
|
+
) -> None:
|
|
75
|
+
object.__setattr__(self, "action", "add_relationship")
|
|
76
|
+
object.__setattr__(self, "object_id", object_id)
|
|
77
|
+
object.__setattr__(self, "object_type", None)
|
|
78
|
+
object.__setattr__(
|
|
79
|
+
self,
|
|
80
|
+
"payload",
|
|
81
|
+
{"relation": relation, "target_id": target_id, "target_type": target_type},
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
@dataclass(frozen=True)
|
|
86
|
+
class RemoveRelationship(Command):
|
|
87
|
+
def __init__(self, object_id: str, relation: str, target_id: str) -> None:
|
|
88
|
+
object.__setattr__(self, "action", "remove_relationship")
|
|
89
|
+
object.__setattr__(self, "object_id", object_id)
|
|
90
|
+
object.__setattr__(self, "object_type", None)
|
|
91
|
+
object.__setattr__(
|
|
92
|
+
self,
|
|
93
|
+
"payload",
|
|
94
|
+
{"relation": relation, "target_id": target_id},
|
|
95
|
+
)
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# Copyright 2026 Brafford Labs, LLC
|
|
3
|
+
"""Command execution context for correlation and idempotency."""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from dataclasses import dataclass, field
|
|
8
|
+
from uuid import uuid4
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@dataclass(frozen=True)
|
|
12
|
+
class CommandContext:
|
|
13
|
+
command_id: str = field(default_factory=lambda: str(uuid4()))
|
|
14
|
+
correlation_id: str = field(default_factory=lambda: str(uuid4()))
|
|
15
|
+
idempotency_key: str | None = None
|
|
16
|
+
application_id: str | None = None
|
|
17
|
+
expected_version: int | None = None
|
|
18
|
+
mandate_id: str | None = None
|
|
19
|
+
purpose: str | None = None
|
|
20
|
+
|
|
21
|
+
def to_metadata(self) -> dict:
|
|
22
|
+
meta = {
|
|
23
|
+
"command_id": self.command_id,
|
|
24
|
+
"correlation_id": self.correlation_id,
|
|
25
|
+
"idempotency_key": self.idempotency_key,
|
|
26
|
+
}
|
|
27
|
+
if self.application_id is not None:
|
|
28
|
+
meta["application_id"] = self.application_id
|
|
29
|
+
if self.expected_version is not None:
|
|
30
|
+
meta["expected_version"] = self.expected_version
|
|
31
|
+
if self.mandate_id is not None:
|
|
32
|
+
meta["mandate_id"] = self.mandate_id
|
|
33
|
+
if self.purpose is not None:
|
|
34
|
+
meta["purpose"] = self.purpose
|
|
35
|
+
return meta
|