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,1919 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# Copyright 2026 Brafford Labs, LLC
|
|
3
|
+
"""Governed Object runtime — single command execution pipeline."""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
from governed_objects.applications.registry import ApplicationDefinition, ApplicationRegistry
|
|
10
|
+
from governed_objects.adapters.context import AdapterContext, AdapterExecuteResult, AdapterFailure
|
|
11
|
+
from governed_objects.adapters.registry import AdapterRegistry
|
|
12
|
+
from governed_objects.domain.actors import ActorRegistry
|
|
13
|
+
from governed_objects.domain.command import (
|
|
14
|
+
AddRelationship,
|
|
15
|
+
Command,
|
|
16
|
+
CreateObject,
|
|
17
|
+
ReadObject,
|
|
18
|
+
RemoveRelationship,
|
|
19
|
+
TransitionState,
|
|
20
|
+
)
|
|
21
|
+
from governed_objects.domain.models import (
|
|
22
|
+
Actor,
|
|
23
|
+
ActorType,
|
|
24
|
+
CommandResult,
|
|
25
|
+
Decision,
|
|
26
|
+
GovernedObject,
|
|
27
|
+
GovernanceEvent,
|
|
28
|
+
Relationship,
|
|
29
|
+
ValidationCheck,
|
|
30
|
+
)
|
|
31
|
+
from governed_objects.delegation.projection import MandateProjection
|
|
32
|
+
from governed_objects.delegation.configuration import DelegationConfiguration, DelegationConfigurationError
|
|
33
|
+
from governed_objects.delegation.materializer import materialize_mandate
|
|
34
|
+
from governed_objects.delegation.mandate import DelegatedMandate
|
|
35
|
+
from governed_objects.delegation.resolver import MandateResolver, ResolvedMandateAuthority
|
|
36
|
+
from governed_objects.delegation.service import MandateService
|
|
37
|
+
from governed_objects.delegation.validator import DelegationSpec, DelegationValidator
|
|
38
|
+
from governed_objects.domain.clock import Clock, SystemClock
|
|
39
|
+
from governed_objects.domain.command_context import CommandContext
|
|
40
|
+
from governed_objects.domain.traversal import TraversalResult
|
|
41
|
+
from governed_objects.events.replay import EventReplayer, ReplayRefused, bound_type_definition
|
|
42
|
+
from governed_objects.idempotency.store import IdempotencyStore
|
|
43
|
+
from governed_objects.interchange.governance import TypeScopedGovernance
|
|
44
|
+
from governed_objects.materialization.service import MaterializationService
|
|
45
|
+
from governed_objects.persistence.sqlite import INT64_MAX, SQLiteStore
|
|
46
|
+
from governed_objects.policy.context import AuthorizationContext, AuthorizationContextError
|
|
47
|
+
from governed_objects.policy.evaluator import CapabilityPolicyEvaluator, PolicyEvaluator
|
|
48
|
+
from governed_objects.policy.outcomes import (
|
|
49
|
+
REFUSAL_DENY_REASONS,
|
|
50
|
+
REFUSED,
|
|
51
|
+
OutcomeCategory,
|
|
52
|
+
classify_outcome,
|
|
53
|
+
)
|
|
54
|
+
from governed_objects.policy.reasons import ConflictReason, DenyReason
|
|
55
|
+
from governed_objects.relationships.canonical import (
|
|
56
|
+
canonical_relationship,
|
|
57
|
+
is_canonical,
|
|
58
|
+
list_canonical_for_object,
|
|
59
|
+
replace_canonical_for_object,
|
|
60
|
+
)
|
|
61
|
+
from governed_objects.providers.errors import ProviderError
|
|
62
|
+
from governed_objects.providers.registry import ProviderRegistry
|
|
63
|
+
from governed_objects.query.service import QueryService
|
|
64
|
+
from governed_objects.query.traversal import GraphTraversalService
|
|
65
|
+
from governed_objects.registration.detach import detach_from_provider
|
|
66
|
+
from governed_objects.registration.service import RegistrationService
|
|
67
|
+
from governed_objects.relationships.sync import RelationshipSyncService
|
|
68
|
+
from governed_objects.observations.ingestion import ObservationIngestionService
|
|
69
|
+
from governed_objects.observations.store import ObservationStore
|
|
70
|
+
from governed_objects.outbound.service import OutboundReconciliationService
|
|
71
|
+
from governed_objects.outbound.store import RepairStore
|
|
72
|
+
from governed_objects.reconciliation.service import ReconciliationService
|
|
73
|
+
from governed_objects.types.extensions import TypeExtensionRegistry
|
|
74
|
+
from governed_objects.types.lifecycle import LifecycleValidator
|
|
75
|
+
from governed_objects.types.registry import TypeRegistry
|
|
76
|
+
from governed_objects.types.validation import SchemaValidator
|
|
77
|
+
|
|
78
|
+
class GovernedObjectRuntime:
|
|
79
|
+
"""Shared runtime for all governed object types."""
|
|
80
|
+
|
|
81
|
+
@classmethod
|
|
82
|
+
def configured(
|
|
83
|
+
cls,
|
|
84
|
+
*,
|
|
85
|
+
store: SQLiteStore,
|
|
86
|
+
type_registry: TypeRegistry,
|
|
87
|
+
adapters: list,
|
|
88
|
+
extension_registry: TypeExtensionRegistry,
|
|
89
|
+
providers: ProviderRegistry,
|
|
90
|
+
application_registry: ApplicationRegistry,
|
|
91
|
+
policy: PolicyEvaluator | None = None,
|
|
92
|
+
actors: ActorRegistry | None = None,
|
|
93
|
+
clock: Clock | None = None,
|
|
94
|
+
governance: TypeScopedGovernance | None = None,
|
|
95
|
+
delegation: DelegationConfiguration | None = None,
|
|
96
|
+
) -> GovernedObjectRuntime:
|
|
97
|
+
"""Construct the common runtime with explicit, possibly empty dependencies.
|
|
98
|
+
|
|
99
|
+
No historical configuration factories are selected on this path. Optional
|
|
100
|
+
profile helpers still need their own supported representations and authority.
|
|
101
|
+
"""
|
|
102
|
+
return cls(
|
|
103
|
+
store=store, type_registry=type_registry, adapters=adapters,
|
|
104
|
+
extension_registry=extension_registry, providers=providers,
|
|
105
|
+
application_registry=application_registry, policy=policy, actors=actors,
|
|
106
|
+
clock=clock, governance=governance, delegation=delegation,
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
def __init__(
|
|
110
|
+
self,
|
|
111
|
+
store: SQLiteStore,
|
|
112
|
+
*,
|
|
113
|
+
type_registry: TypeRegistry,
|
|
114
|
+
adapters: list,
|
|
115
|
+
extension_registry: TypeExtensionRegistry,
|
|
116
|
+
providers: ProviderRegistry,
|
|
117
|
+
application_registry: ApplicationRegistry,
|
|
118
|
+
policy: PolicyEvaluator | None = None,
|
|
119
|
+
actors: ActorRegistry | None = None,
|
|
120
|
+
clock: Clock | None = None,
|
|
121
|
+
governance: TypeScopedGovernance | None = None,
|
|
122
|
+
delegation: DelegationConfiguration | None = None,
|
|
123
|
+
) -> None:
|
|
124
|
+
for name, value in (
|
|
125
|
+
("store", store), ("type_registry", type_registry), ("adapters", adapters),
|
|
126
|
+
("extension_registry", extension_registry), ("providers", providers),
|
|
127
|
+
("application_registry", application_registry),
|
|
128
|
+
):
|
|
129
|
+
if value is None:
|
|
130
|
+
raise ValueError(f"runtime requires non-None {name}")
|
|
131
|
+
self._store = store
|
|
132
|
+
self._governance = governance
|
|
133
|
+
self._clock = clock or SystemClock()
|
|
134
|
+
self._types = type_registry
|
|
135
|
+
self._extensions = extension_registry
|
|
136
|
+
self._policy = policy or CapabilityPolicyEvaluator(
|
|
137
|
+
clock=self._clock, store=store
|
|
138
|
+
)
|
|
139
|
+
self._applications = application_registry
|
|
140
|
+
self._actors = actors
|
|
141
|
+
self._delegation = delegation
|
|
142
|
+
self._mandate_resolver = MandateResolver(
|
|
143
|
+
store, clock=self._clock, validate_binding=self._validate_delegation_binding,
|
|
144
|
+
scope_decoder=self._decode_legacy_scope,
|
|
145
|
+
)
|
|
146
|
+
self._delegation_validator = DelegationValidator(store)
|
|
147
|
+
self._mandate_projection = MandateProjection(store, resolver=self._mandate_resolver)
|
|
148
|
+
self._mandate_service = MandateService(
|
|
149
|
+
self,
|
|
150
|
+
self._mandate_resolver,
|
|
151
|
+
self._delegation_validator,
|
|
152
|
+
actors,
|
|
153
|
+
)
|
|
154
|
+
self._lifecycle = LifecycleValidator()
|
|
155
|
+
self._schema = SchemaValidator()
|
|
156
|
+
self._providers = providers
|
|
157
|
+
self._adapters_list = adapters
|
|
158
|
+
self._adapters = AdapterRegistry(self._adapters_list)
|
|
159
|
+
if delegation is not None:
|
|
160
|
+
self.require_delegation()
|
|
161
|
+
if isinstance(self._policy, CapabilityPolicyEvaluator):
|
|
162
|
+
self._policy.bind_context_sources(
|
|
163
|
+
application_source=self._applications.get,
|
|
164
|
+
mandate_source=self._mandate_resolver.resolve_authority,
|
|
165
|
+
)
|
|
166
|
+
self._context = AdapterContext(store, self._providers, self._types)
|
|
167
|
+
self._rel_sync = RelationshipSyncService(store)
|
|
168
|
+
self._materializer = MaterializationService(
|
|
169
|
+
store, self._adapters, self._types, self._rel_sync
|
|
170
|
+
)
|
|
171
|
+
self._observation_store = ObservationStore(store)
|
|
172
|
+
self._repair_store = RepairStore(store)
|
|
173
|
+
self._outbound = OutboundReconciliationService(
|
|
174
|
+
store, self._adapters, self._providers, self._types, self._repair_store
|
|
175
|
+
)
|
|
176
|
+
self._observations = ObservationIngestionService(
|
|
177
|
+
store,
|
|
178
|
+
self._types,
|
|
179
|
+
self._observation_store,
|
|
180
|
+
self._rel_sync,
|
|
181
|
+
self._adapters,
|
|
182
|
+
self._context,
|
|
183
|
+
outbound=self._outbound,
|
|
184
|
+
)
|
|
185
|
+
self._registration = RegistrationService(
|
|
186
|
+
store, self._adapters, self._materializer, self._types, self._observations, self._rel_sync
|
|
187
|
+
)
|
|
188
|
+
self._replayer = EventReplayer(store, self._extensions, self._types)
|
|
189
|
+
self._reconciliation = ReconciliationService(store, self._adapters, self._observations)
|
|
190
|
+
self._idempotency = IdempotencyStore(store)
|
|
191
|
+
self._query = QueryService(
|
|
192
|
+
store, self._types, self._policy, self._materializer, self._context,
|
|
193
|
+
resolve_auth=self.resolve_authorization_context,
|
|
194
|
+
)
|
|
195
|
+
self._traversal = GraphTraversalService(
|
|
196
|
+
store, self._types, self._policy, self._adapters_list, self._materializer, self._context,
|
|
197
|
+
resolve_auth=self.resolve_authorization_context,
|
|
198
|
+
)
|
|
199
|
+
|
|
200
|
+
@property
|
|
201
|
+
def providers(self) -> ProviderRegistry:
|
|
202
|
+
return self._providers
|
|
203
|
+
|
|
204
|
+
@property
|
|
205
|
+
def registration(self) -> RegistrationService:
|
|
206
|
+
return self._registration
|
|
207
|
+
|
|
208
|
+
@property
|
|
209
|
+
def context(self) -> AdapterContext:
|
|
210
|
+
return self._context
|
|
211
|
+
|
|
212
|
+
@property
|
|
213
|
+
def mandates(self) -> MandateService:
|
|
214
|
+
return self._mandate_service
|
|
215
|
+
|
|
216
|
+
@property
|
|
217
|
+
def mandate_resolver(self) -> MandateResolver:
|
|
218
|
+
return self._mandate_resolver
|
|
219
|
+
|
|
220
|
+
@property
|
|
221
|
+
def mandate_projection(self) -> MandateProjection:
|
|
222
|
+
return self._mandate_projection
|
|
223
|
+
|
|
224
|
+
def _make_context(self, **kwargs) -> CommandContext:
|
|
225
|
+
return CommandContext(**kwargs)
|
|
226
|
+
|
|
227
|
+
@property
|
|
228
|
+
def clock(self) -> Clock:
|
|
229
|
+
return self._clock
|
|
230
|
+
|
|
231
|
+
def execute(
|
|
232
|
+
self,
|
|
233
|
+
command: Command,
|
|
234
|
+
*,
|
|
235
|
+
actor: Actor,
|
|
236
|
+
command_context: CommandContext | None = None,
|
|
237
|
+
) -> CommandResult:
|
|
238
|
+
result = self._dispatch(command, actor, command_context)
|
|
239
|
+
return self._shape_for_requester(result, command, actor, command_context)
|
|
240
|
+
|
|
241
|
+
# Protected-content boundary (Draft 2.2 AUTH-DISCLOSE-001). Every caller-
|
|
242
|
+
# visible CommandResult passes through here. Stored audit rows are not
|
|
243
|
+
# affected; only the returned result is shaped.
|
|
244
|
+
_GENERIC_REJECTION_REASON = {
|
|
245
|
+
OutcomeCategory.CONFLICT: "version conflict",
|
|
246
|
+
OutcomeCategory.SEMANTICALLY_INVALID: "command rejected",
|
|
247
|
+
REFUSED: "command refused",
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
def _shape_for_requester(
|
|
251
|
+
self,
|
|
252
|
+
result: CommandResult,
|
|
253
|
+
command: Command,
|
|
254
|
+
actor: Actor,
|
|
255
|
+
command_context: CommandContext | None,
|
|
256
|
+
) -> CommandResult:
|
|
257
|
+
result = self._finalize(result)
|
|
258
|
+
if result.decision == Decision.ALLOW:
|
|
259
|
+
return result
|
|
260
|
+
stored = self._store.get(command.object_id) if command.object_id else None
|
|
261
|
+
if stored is None or self._requester_can_read(actor, stored, command_context):
|
|
262
|
+
return result
|
|
263
|
+
result.object_version = None
|
|
264
|
+
result.actual_version = None
|
|
265
|
+
result.previous_state = None
|
|
266
|
+
result.new_state = None
|
|
267
|
+
result.data = {}
|
|
268
|
+
result.events = []
|
|
269
|
+
result.validation_checks = [
|
|
270
|
+
ValidationCheck(name=c.name, passed=c.passed, message="") for c in result.validation_checks
|
|
271
|
+
] if self._types.supports_type_version(stored.type, stored.type_version) else []
|
|
272
|
+
if result.outcome_category != OutcomeCategory.AUTHORIZATION_DENIED:
|
|
273
|
+
result.reason = self._GENERIC_REJECTION_REASON.get(
|
|
274
|
+
result.outcome_category, "command rejected"
|
|
275
|
+
)
|
|
276
|
+
return result
|
|
277
|
+
|
|
278
|
+
def _requester_can_read(
|
|
279
|
+
self,
|
|
280
|
+
actor: Actor,
|
|
281
|
+
obj: GovernedObject,
|
|
282
|
+
command_context: CommandContext | None,
|
|
283
|
+
) -> bool:
|
|
284
|
+
# Replacement semantics cannot grant permission to reveal diagnostics.
|
|
285
|
+
if not self._types.supports_type_version(obj.type, obj.type_version):
|
|
286
|
+
return False
|
|
287
|
+
try:
|
|
288
|
+
type_def = self._types.get(obj.type)
|
|
289
|
+
except KeyError:
|
|
290
|
+
return False
|
|
291
|
+
auth_ctx, auth_deny = self._resolve_auth(command_context)
|
|
292
|
+
if auth_deny is not None:
|
|
293
|
+
return False
|
|
294
|
+
decision = self._policy.evaluate(
|
|
295
|
+
actor,
|
|
296
|
+
"read",
|
|
297
|
+
obj,
|
|
298
|
+
type_def,
|
|
299
|
+
application=auth_ctx.application if auth_ctx else None,
|
|
300
|
+
mandate=auth_ctx.mandate if auth_ctx else None,
|
|
301
|
+
resolved_mandate=auth_ctx.resolved_mandate if auth_ctx else None,
|
|
302
|
+
purpose=auth_ctx.purpose if auth_ctx else None,
|
|
303
|
+
)
|
|
304
|
+
return decision.decision == Decision.ALLOW
|
|
305
|
+
|
|
306
|
+
def _dispatch(
|
|
307
|
+
self,
|
|
308
|
+
command: Command,
|
|
309
|
+
actor: Actor,
|
|
310
|
+
command_context: CommandContext | None,
|
|
311
|
+
) -> CommandResult:
|
|
312
|
+
if isinstance(command, CreateObject):
|
|
313
|
+
return self._execute_create(command, actor, command_context)
|
|
314
|
+
if isinstance(command, ReadObject):
|
|
315
|
+
return self._execute_read(command, actor, command_context)
|
|
316
|
+
if isinstance(command, AddRelationship):
|
|
317
|
+
return self._execute_add_relationship(command, actor, command_context)
|
|
318
|
+
if isinstance(command, RemoveRelationship):
|
|
319
|
+
return self._execute_remove_relationship(command, actor, command_context)
|
|
320
|
+
if isinstance(command, TransitionState) or command.object_id:
|
|
321
|
+
return self._execute_transition(command, actor, command_context)
|
|
322
|
+
return self._finalize(
|
|
323
|
+
CommandResult(decision=Decision.DENY, reason="unknown command", action=command.action)
|
|
324
|
+
)
|
|
325
|
+
|
|
326
|
+
def get_object(self, object_id: str, *, raw: bool = False) -> GovernedObject | None:
|
|
327
|
+
"""Admin/stored view. Use materialize() for current canonical state."""
|
|
328
|
+
if raw:
|
|
329
|
+
return self._store.get(object_id)
|
|
330
|
+
return self._materializer.materialize(
|
|
331
|
+
object_id, self._context, hydrate=False, persist=False
|
|
332
|
+
)
|
|
333
|
+
|
|
334
|
+
def materialize(
|
|
335
|
+
self,
|
|
336
|
+
object_id: str,
|
|
337
|
+
actor: Actor | None = None,
|
|
338
|
+
*,
|
|
339
|
+
field_filter: bool = False,
|
|
340
|
+
fields: list[str] | None = None,
|
|
341
|
+
) -> GovernedObject | None:
|
|
342
|
+
"""Unified canonical materialization — hydrate + optional field filter."""
|
|
343
|
+
try:
|
|
344
|
+
return self._materializer.materialize(
|
|
345
|
+
object_id,
|
|
346
|
+
self._context,
|
|
347
|
+
actor,
|
|
348
|
+
hydrate=True,
|
|
349
|
+
field_filter=field_filter,
|
|
350
|
+
fields=fields,
|
|
351
|
+
persist=True,
|
|
352
|
+
)
|
|
353
|
+
except ProviderError as exc:
|
|
354
|
+
return None
|
|
355
|
+
|
|
356
|
+
def replay(self, object_id: str, *, allow_legacy: bool = False) -> GovernedObject | None:
|
|
357
|
+
"""Stage supported reconstruction before installing state and edges.
|
|
358
|
+
|
|
359
|
+
ReplayRefused is content-free; None means only a missing target.
|
|
360
|
+
Legacy opt-in changes ordering, not binding requirements. Supported
|
|
361
|
+
no-history/no-reducer/decline fallback is not reconstruction proof.
|
|
362
|
+
"""
|
|
363
|
+
stored = self._store.get(object_id)
|
|
364
|
+
if stored is None:
|
|
365
|
+
return None
|
|
366
|
+
if not self._replayer.check_type_version_supported(stored):
|
|
367
|
+
raise ReplayRefused()
|
|
368
|
+
obj = self._replayer.reconstruct(object_id, allow_legacy=allow_legacy)
|
|
369
|
+
if obj is None:
|
|
370
|
+
return None
|
|
371
|
+
rels = self._replayer.reconstruct_relationships(object_id, allow_legacy=allow_legacy)
|
|
372
|
+
self._replayer._check_result(stored, obj)
|
|
373
|
+
if not self._replayer.check_type_version_supported(stored) or any(
|
|
374
|
+
rel.source_id != object_id for rel in rels
|
|
375
|
+
):
|
|
376
|
+
raise ReplayRefused()
|
|
377
|
+
self._store.save(obj)
|
|
378
|
+
replace_canonical_for_object(self._store, object_id, rels)
|
|
379
|
+
return obj
|
|
380
|
+
|
|
381
|
+
def canonical_relationships(self, object_id: str) -> list[Relationship]:
|
|
382
|
+
return list_canonical_for_object(self._store, object_id)
|
|
383
|
+
|
|
384
|
+
def add_relationship(
|
|
385
|
+
self,
|
|
386
|
+
object_id: str,
|
|
387
|
+
relation: str,
|
|
388
|
+
target_id: str,
|
|
389
|
+
*,
|
|
390
|
+
actor: Actor,
|
|
391
|
+
target_type: str | None = None,
|
|
392
|
+
command_context: CommandContext | None = None,
|
|
393
|
+
) -> CommandResult:
|
|
394
|
+
return self.execute(
|
|
395
|
+
AddRelationship(object_id, relation, target_id, target_type=target_type),
|
|
396
|
+
actor=actor,
|
|
397
|
+
command_context=command_context,
|
|
398
|
+
)
|
|
399
|
+
|
|
400
|
+
def remove_relationship(
|
|
401
|
+
self,
|
|
402
|
+
object_id: str,
|
|
403
|
+
relation: str,
|
|
404
|
+
target_id: str,
|
|
405
|
+
*,
|
|
406
|
+
actor: Actor,
|
|
407
|
+
command_context: CommandContext | None = None,
|
|
408
|
+
) -> CommandResult:
|
|
409
|
+
return self.execute(
|
|
410
|
+
RemoveRelationship(object_id, relation, target_id),
|
|
411
|
+
actor=actor,
|
|
412
|
+
command_context=command_context,
|
|
413
|
+
)
|
|
414
|
+
|
|
415
|
+
def reconcile(self, object_id: str):
|
|
416
|
+
return self._reconciliation.check(object_id, self._context)
|
|
417
|
+
|
|
418
|
+
def reconcile_and_ingest(self, object_id: str):
|
|
419
|
+
return self._reconciliation.reconcile_and_ingest(object_id, self._context)
|
|
420
|
+
|
|
421
|
+
def ingest_observation(self, observation):
|
|
422
|
+
return self._observations.ingest(observation)
|
|
423
|
+
|
|
424
|
+
def resolve_review(self, case_id: str, *, resolution: str, resolved_by: str):
|
|
425
|
+
return self._observations.resolve_review(
|
|
426
|
+
case_id, resolution=resolution, resolved_by=resolved_by
|
|
427
|
+
)
|
|
428
|
+
|
|
429
|
+
@property
|
|
430
|
+
def observation_ingestion(self) -> ObservationIngestionService:
|
|
431
|
+
return self._observations
|
|
432
|
+
|
|
433
|
+
@property
|
|
434
|
+
def outbound(self) -> OutboundReconciliationService:
|
|
435
|
+
return self._outbound
|
|
436
|
+
|
|
437
|
+
def plan_repair(self, object_id: str, *, fields: list[str] | None = None):
|
|
438
|
+
return self._outbound.plan(object_id, self._context, fields=fields)
|
|
439
|
+
|
|
440
|
+
def apply_repair(self, plan, *, initiated_by: str = "system", simulate_outcome_unknown: bool = False):
|
|
441
|
+
return self._outbound.apply(
|
|
442
|
+
plan,
|
|
443
|
+
self._context,
|
|
444
|
+
initiated_by=initiated_by,
|
|
445
|
+
simulate_outcome_unknown=simulate_outcome_unknown,
|
|
446
|
+
)
|
|
447
|
+
|
|
448
|
+
def replace_provider(self, object_id: str, *, new_provider_name: str, new_external_id: str):
|
|
449
|
+
return self._outbound.replace_provider(
|
|
450
|
+
object_id,
|
|
451
|
+
new_provider_name=new_provider_name,
|
|
452
|
+
new_external_id=new_external_id,
|
|
453
|
+
context=self._context,
|
|
454
|
+
)
|
|
455
|
+
|
|
456
|
+
def query(
|
|
457
|
+
self,
|
|
458
|
+
actor: Actor,
|
|
459
|
+
*,
|
|
460
|
+
object_type: str | None = None,
|
|
461
|
+
object_types: list[str] | None = None,
|
|
462
|
+
state: str | None = None,
|
|
463
|
+
include_subtypes: bool = False,
|
|
464
|
+
auth: AuthorizationContext | None = None,
|
|
465
|
+
) -> list[GovernedObject]:
|
|
466
|
+
return self._query.query(
|
|
467
|
+
actor,
|
|
468
|
+
object_type=object_type,
|
|
469
|
+
object_types=object_types,
|
|
470
|
+
state=state,
|
|
471
|
+
include_subtypes=include_subtypes,
|
|
472
|
+
auth=auth,
|
|
473
|
+
)
|
|
474
|
+
|
|
475
|
+
def detach_provider(self, object_id: str) -> GovernedObject | None:
|
|
476
|
+
return detach_from_provider(self._store, self._materializer, self._context, object_id)
|
|
477
|
+
|
|
478
|
+
def history(self, object_id: str) -> list[GovernanceEvent]:
|
|
479
|
+
return self._store.list_for_object(object_id)
|
|
480
|
+
|
|
481
|
+
def accepted_history(self, object_id: str) -> list[GovernanceEvent]:
|
|
482
|
+
"""Accepted canonical changes in recorded per-object position order.
|
|
483
|
+
|
|
484
|
+
EXPERIMENTAL. Selects rows carrying
|
|
485
|
+
accepted-history positions, unlike timestamp-ordered ``history()``
|
|
486
|
+
event/audit rows. Replay defaults to positioned accepted revisions;
|
|
487
|
+
unpositioned accepted history requires explicit ``allow_legacy=True``
|
|
488
|
+
for legacy timestamp ordering. Exact binding support still applies;
|
|
489
|
+
supported no-history/no-reducer/decline fallbacks are not proof of
|
|
490
|
+
reconstruction.
|
|
491
|
+
"""
|
|
492
|
+
return self._store.list_accepted_for_object(object_id)
|
|
493
|
+
|
|
494
|
+
def export_interchange_bundle(
|
|
495
|
+
self,
|
|
496
|
+
identities: list[str],
|
|
497
|
+
*,
|
|
498
|
+
actor: Actor,
|
|
499
|
+
closed: bool,
|
|
500
|
+
include_timestamps: bool = False,
|
|
501
|
+
source_implementation: str | None = None,
|
|
502
|
+
) -> dict[str, Any]:
|
|
503
|
+
"""EXPERIMENTAL: export ``go-core-interchange-experiment/2``. Raises
|
|
504
|
+
``ExportRefused`` instead of emitting a repaired bundle."""
|
|
505
|
+
from governed_objects.interchange.export import InterchangeExporter
|
|
506
|
+
|
|
507
|
+
return InterchangeExporter(self, self._store, self._types, self._policy).export(
|
|
508
|
+
identities,
|
|
509
|
+
actor=actor,
|
|
510
|
+
closed=closed,
|
|
511
|
+
include_timestamps=include_timestamps,
|
|
512
|
+
source_implementation=source_implementation,
|
|
513
|
+
)
|
|
514
|
+
|
|
515
|
+
def import_interchange_bundle(self, bundle, *, actor: Actor, provenance=None):
|
|
516
|
+
"""EXPERIMENTAL privileged new-target import.
|
|
517
|
+
|
|
518
|
+
``bundle`` is a semantic ``ImportBundle``. Restores accepted history as
|
|
519
|
+
history in one transaction, or raises ``ImportRefused`` with the store
|
|
520
|
+
unchanged. Requires the local import grant; historical actors are
|
|
521
|
+
evidence only and are never authorized."""
|
|
522
|
+
from governed_objects.interchange.importer import InterchangeImporter
|
|
523
|
+
|
|
524
|
+
return InterchangeImporter(self._store, self._types, self._governance, self._clock).import_bundle(
|
|
525
|
+
bundle, actor=actor, provenance=provenance
|
|
526
|
+
)
|
|
527
|
+
|
|
528
|
+
def prc_declaration(self) -> dict[str, Any]:
|
|
529
|
+
"""EXPERIMENTAL: this runtime's Draft 2.2 Portable Restoration Continuity
|
|
530
|
+
role, capability, evidence-level, and handoff declaration for the export
|
|
531
|
+
and import APIs above, with type bindings from the configured registry."""
|
|
532
|
+
from governed_objects.interchange.prc import prc_declaration
|
|
533
|
+
|
|
534
|
+
return prc_declaration(self._types)
|
|
535
|
+
|
|
536
|
+
def interchange_imports(self) -> list[dict[str, Any]]:
|
|
537
|
+
"""EXPERIMENTAL: import provenance records (Operational Metadata)."""
|
|
538
|
+
return self._store.list_imports()
|
|
539
|
+
|
|
540
|
+
def related(
|
|
541
|
+
self,
|
|
542
|
+
actor: Actor,
|
|
543
|
+
object_id: str,
|
|
544
|
+
*,
|
|
545
|
+
auth: AuthorizationContext | None = None,
|
|
546
|
+
) -> list[GovernedObject]:
|
|
547
|
+
return self._query.related(actor, object_id, auth=auth)
|
|
548
|
+
|
|
549
|
+
def traverse(
|
|
550
|
+
self,
|
|
551
|
+
actor: Actor,
|
|
552
|
+
start_object_id: str,
|
|
553
|
+
*,
|
|
554
|
+
max_depth: int = 3,
|
|
555
|
+
relations: list[str] | None = None,
|
|
556
|
+
auth: AuthorizationContext | None = None,
|
|
557
|
+
) -> list[TraversalResult]:
|
|
558
|
+
return self._traversal.traverse(
|
|
559
|
+
actor,
|
|
560
|
+
start_object_id,
|
|
561
|
+
max_depth=max_depth,
|
|
562
|
+
relations=relations,
|
|
563
|
+
auth=auth,
|
|
564
|
+
)
|
|
565
|
+
|
|
566
|
+
def _finalize(self, result: CommandResult) -> CommandResult:
|
|
567
|
+
if result.outcome_category is not None:
|
|
568
|
+
return result
|
|
569
|
+
if result.decision == Decision.DENY and result.deny_reason in REFUSAL_DENY_REASONS:
|
|
570
|
+
return result.model_copy(update={"outcome_category": REFUSED})
|
|
571
|
+
category = classify_outcome(
|
|
572
|
+
result.decision,
|
|
573
|
+
deny_reason=result.deny_reason,
|
|
574
|
+
conflict_reason=result.conflict_reason,
|
|
575
|
+
has_validation_checks=bool(result.validation_checks),
|
|
576
|
+
)
|
|
577
|
+
return result.model_copy(update={"outcome_category": category.value})
|
|
578
|
+
|
|
579
|
+
def _execute_add_relationship(
|
|
580
|
+
self,
|
|
581
|
+
command: AddRelationship,
|
|
582
|
+
actor: Actor,
|
|
583
|
+
command_context: CommandContext | None = None,
|
|
584
|
+
) -> CommandResult:
|
|
585
|
+
stored = self._store.get(command.object_id or "")
|
|
586
|
+
if stored is None:
|
|
587
|
+
return self._finalize(
|
|
588
|
+
CommandResult(
|
|
589
|
+
decision=Decision.DENY,
|
|
590
|
+
reason="not found",
|
|
591
|
+
action="add_relationship",
|
|
592
|
+
deny_reason=DenyReason.NOT_FOUND.value,
|
|
593
|
+
)
|
|
594
|
+
)
|
|
595
|
+
|
|
596
|
+
if not self._types.supports_type_version(stored.type, stored.type_version):
|
|
597
|
+
return self._deny(
|
|
598
|
+
stored.id,
|
|
599
|
+
stored.type,
|
|
600
|
+
actor,
|
|
601
|
+
"add_relationship",
|
|
602
|
+
"unsupported type version",
|
|
603
|
+
stored.state,
|
|
604
|
+
stored.state,
|
|
605
|
+
deny_reason=DenyReason.TYPE_VERSION_UNSUPPORTED,
|
|
606
|
+
command_context=command_context,
|
|
607
|
+
)
|
|
608
|
+
|
|
609
|
+
relation = command.payload.get("relation", "")
|
|
610
|
+
target_id = command.payload.get("target_id", "")
|
|
611
|
+
target_type = command.payload.get("target_type")
|
|
612
|
+
if not relation or not target_id:
|
|
613
|
+
return self._deny(
|
|
614
|
+
stored.id,
|
|
615
|
+
stored.type,
|
|
616
|
+
actor,
|
|
617
|
+
"add_relationship",
|
|
618
|
+
"relationship requires type and target",
|
|
619
|
+
stored.state,
|
|
620
|
+
stored.state,
|
|
621
|
+
deny_reason=DenyReason.RELATIONSHIP_INVALID,
|
|
622
|
+
command_context=command_context,
|
|
623
|
+
)
|
|
624
|
+
|
|
625
|
+
type_def = self._types.get(stored.type)
|
|
626
|
+
auth, auth_deny = self._resolve_auth(command_context)
|
|
627
|
+
if auth_deny is not None:
|
|
628
|
+
return auth_deny
|
|
629
|
+
|
|
630
|
+
policy_decision = self._policy.evaluate(
|
|
631
|
+
actor,
|
|
632
|
+
"add_relationship",
|
|
633
|
+
stored,
|
|
634
|
+
type_def,
|
|
635
|
+
application=auth.application if auth else None,
|
|
636
|
+
mandate=auth.mandate if auth else None,
|
|
637
|
+
resolved_mandate=auth.resolved_mandate if auth else None,
|
|
638
|
+
purpose=auth.purpose if auth else None,
|
|
639
|
+
)
|
|
640
|
+
if policy_decision.decision == Decision.DENY:
|
|
641
|
+
return self._deny(
|
|
642
|
+
stored.id,
|
|
643
|
+
stored.type,
|
|
644
|
+
actor,
|
|
645
|
+
"add_relationship",
|
|
646
|
+
policy_decision.reason,
|
|
647
|
+
stored.state,
|
|
648
|
+
stored.state,
|
|
649
|
+
deny_reason=policy_decision.deny_reason,
|
|
650
|
+
command_context=command_context,
|
|
651
|
+
)
|
|
652
|
+
|
|
653
|
+
conflict = self._check_version_conflict(
|
|
654
|
+
stored, "add_relationship", actor, command_context
|
|
655
|
+
)
|
|
656
|
+
if conflict is not None:
|
|
657
|
+
return conflict
|
|
658
|
+
|
|
659
|
+
existing = list_canonical_for_object(self._store, stored.id)
|
|
660
|
+
prospective = existing if any(r.relation == relation and r.target_id == target_id for r in existing) else (
|
|
661
|
+
existing + [canonical_relationship(stored.id, relation, target_id, target_type=target_type)]
|
|
662
|
+
)
|
|
663
|
+
admission = self._validate_mandate_candidate(stored, prospective, actor, 'add_relationship', command_context)
|
|
664
|
+
if admission is not None:
|
|
665
|
+
return admission
|
|
666
|
+
if any(r.relation == relation and r.target_id == target_id for r in existing):
|
|
667
|
+
return self._finalize(
|
|
668
|
+
CommandResult(
|
|
669
|
+
decision=Decision.ALLOW,
|
|
670
|
+
reason="relationship already present",
|
|
671
|
+
action="add_relationship",
|
|
672
|
+
object_id=stored.id,
|
|
673
|
+
object_version=stored.version,
|
|
674
|
+
)
|
|
675
|
+
)
|
|
676
|
+
|
|
677
|
+
with self._store.observation_transaction():
|
|
678
|
+
current = self._store.get(stored.id)
|
|
679
|
+
if current is None:
|
|
680
|
+
return self._finalize(
|
|
681
|
+
CommandResult(
|
|
682
|
+
decision=Decision.DENY,
|
|
683
|
+
reason="not found",
|
|
684
|
+
action="add_relationship",
|
|
685
|
+
deny_reason=DenyReason.NOT_FOUND.value,
|
|
686
|
+
)
|
|
687
|
+
)
|
|
688
|
+
conflict = self._check_version_conflict(
|
|
689
|
+
current, "add_relationship", actor, command_context
|
|
690
|
+
)
|
|
691
|
+
if conflict is not None:
|
|
692
|
+
return conflict
|
|
693
|
+
new_version = self._allocate_version(current)
|
|
694
|
+
if new_version is None:
|
|
695
|
+
return self._version_space_exhausted(current, actor, "add_relationship", command_context)
|
|
696
|
+
rel = canonical_relationship(
|
|
697
|
+
current.id, relation, target_id, target_type=target_type
|
|
698
|
+
)
|
|
699
|
+
updated = current.model_copy(update={"version": new_version})
|
|
700
|
+
self._store.add(rel)
|
|
701
|
+
self._store.save(updated)
|
|
702
|
+
|
|
703
|
+
details = {
|
|
704
|
+
"relation": relation,
|
|
705
|
+
"target_id": target_id,
|
|
706
|
+
"target_type": target_type,
|
|
707
|
+
}
|
|
708
|
+
event = self._allow_event(
|
|
709
|
+
updated,
|
|
710
|
+
actor,
|
|
711
|
+
"add_relationship",
|
|
712
|
+
current.state,
|
|
713
|
+
current.state,
|
|
714
|
+
"relationship added",
|
|
715
|
+
command_context=command_context,
|
|
716
|
+
extra_details=details,
|
|
717
|
+
)
|
|
718
|
+
event = self._store.append_accepted(event)
|
|
719
|
+
return self._finalize(
|
|
720
|
+
CommandResult(
|
|
721
|
+
decision=Decision.ALLOW,
|
|
722
|
+
reason="relationship added",
|
|
723
|
+
action="add_relationship",
|
|
724
|
+
object_id=updated.id,
|
|
725
|
+
object_version=updated.version,
|
|
726
|
+
events=[event],
|
|
727
|
+
)
|
|
728
|
+
)
|
|
729
|
+
|
|
730
|
+
def _execute_remove_relationship(
|
|
731
|
+
self,
|
|
732
|
+
command: RemoveRelationship,
|
|
733
|
+
actor: Actor,
|
|
734
|
+
command_context: CommandContext | None = None,
|
|
735
|
+
) -> CommandResult:
|
|
736
|
+
stored = self._store.get(command.object_id or "")
|
|
737
|
+
if stored is None:
|
|
738
|
+
return self._finalize(
|
|
739
|
+
CommandResult(
|
|
740
|
+
decision=Decision.DENY,
|
|
741
|
+
reason="not found",
|
|
742
|
+
action="remove_relationship",
|
|
743
|
+
deny_reason=DenyReason.NOT_FOUND.value,
|
|
744
|
+
)
|
|
745
|
+
)
|
|
746
|
+
|
|
747
|
+
type_def = bound_type_definition(self._types, stored)
|
|
748
|
+
if type_def is None:
|
|
749
|
+
return self._read_not_authorized(stored.id).model_copy(update={"action": command.action})
|
|
750
|
+
|
|
751
|
+
relation = command.payload.get("relation", "")
|
|
752
|
+
target_id = command.payload.get("target_id", "")
|
|
753
|
+
if not relation or not target_id:
|
|
754
|
+
return self._deny(
|
|
755
|
+
stored.id,
|
|
756
|
+
stored.type,
|
|
757
|
+
actor,
|
|
758
|
+
"remove_relationship",
|
|
759
|
+
"relationship requires type and target",
|
|
760
|
+
stored.state,
|
|
761
|
+
stored.state,
|
|
762
|
+
deny_reason=DenyReason.RELATIONSHIP_INVALID,
|
|
763
|
+
command_context=command_context,
|
|
764
|
+
)
|
|
765
|
+
|
|
766
|
+
auth, auth_deny = self._resolve_auth(command_context)
|
|
767
|
+
if auth_deny is not None:
|
|
768
|
+
return auth_deny
|
|
769
|
+
|
|
770
|
+
policy_decision = self._policy.evaluate(
|
|
771
|
+
actor,
|
|
772
|
+
"remove_relationship",
|
|
773
|
+
stored,
|
|
774
|
+
type_def,
|
|
775
|
+
application=auth.application if auth else None,
|
|
776
|
+
mandate=auth.mandate if auth else None,
|
|
777
|
+
resolved_mandate=auth.resolved_mandate if auth else None,
|
|
778
|
+
purpose=auth.purpose if auth else None,
|
|
779
|
+
)
|
|
780
|
+
if policy_decision.decision == Decision.DENY:
|
|
781
|
+
return self._deny(
|
|
782
|
+
stored.id,
|
|
783
|
+
stored.type,
|
|
784
|
+
actor,
|
|
785
|
+
"remove_relationship",
|
|
786
|
+
policy_decision.reason,
|
|
787
|
+
stored.state,
|
|
788
|
+
stored.state,
|
|
789
|
+
deny_reason=policy_decision.deny_reason,
|
|
790
|
+
command_context=command_context,
|
|
791
|
+
)
|
|
792
|
+
|
|
793
|
+
conflict = self._check_version_conflict(
|
|
794
|
+
stored, "remove_relationship", actor, command_context
|
|
795
|
+
)
|
|
796
|
+
if conflict is not None:
|
|
797
|
+
return conflict
|
|
798
|
+
|
|
799
|
+
existing = [
|
|
800
|
+
r
|
|
801
|
+
for r in self._store.list_for_source(stored.id)
|
|
802
|
+
if is_canonical(r) and r.relation == relation and r.target_id == target_id
|
|
803
|
+
]
|
|
804
|
+
if not existing:
|
|
805
|
+
return self._finalize(
|
|
806
|
+
CommandResult(
|
|
807
|
+
decision=Decision.ALLOW,
|
|
808
|
+
reason="relationship not present",
|
|
809
|
+
action="remove_relationship",
|
|
810
|
+
object_id=stored.id,
|
|
811
|
+
object_version=stored.version,
|
|
812
|
+
)
|
|
813
|
+
)
|
|
814
|
+
|
|
815
|
+
prospective = [r for r in list_canonical_for_object(self._store, stored.id)
|
|
816
|
+
if not (r.relation == relation and r.target_id == target_id)]
|
|
817
|
+
admission = self._validate_mandate_candidate(stored, prospective, actor, 'remove_relationship', command_context)
|
|
818
|
+
if admission is not None:
|
|
819
|
+
return admission
|
|
820
|
+
|
|
821
|
+
with self._store.observation_transaction():
|
|
822
|
+
current = self._store.get(stored.id)
|
|
823
|
+
if current is None:
|
|
824
|
+
return self._finalize(
|
|
825
|
+
CommandResult(
|
|
826
|
+
decision=Decision.DENY,
|
|
827
|
+
reason="not found",
|
|
828
|
+
action="remove_relationship",
|
|
829
|
+
deny_reason=DenyReason.NOT_FOUND.value,
|
|
830
|
+
)
|
|
831
|
+
)
|
|
832
|
+
conflict = self._check_version_conflict(
|
|
833
|
+
current, "remove_relationship", actor, command_context
|
|
834
|
+
)
|
|
835
|
+
if conflict is not None:
|
|
836
|
+
return conflict
|
|
837
|
+
new_version = self._allocate_version(current)
|
|
838
|
+
if new_version is None:
|
|
839
|
+
return self._version_space_exhausted(
|
|
840
|
+
current, actor, "remove_relationship", command_context
|
|
841
|
+
)
|
|
842
|
+
self._store.remove(current.id, relation, target_id)
|
|
843
|
+
updated = current.model_copy(update={"version": new_version})
|
|
844
|
+
self._store.save(updated)
|
|
845
|
+
|
|
846
|
+
details = {"relation": relation, "target_id": target_id}
|
|
847
|
+
event = self._allow_event(
|
|
848
|
+
updated,
|
|
849
|
+
actor,
|
|
850
|
+
"remove_relationship",
|
|
851
|
+
current.state,
|
|
852
|
+
current.state,
|
|
853
|
+
"relationship removed",
|
|
854
|
+
command_context=command_context,
|
|
855
|
+
extra_details=details,
|
|
856
|
+
)
|
|
857
|
+
event = self._store.append_accepted(event)
|
|
858
|
+
return self._finalize(
|
|
859
|
+
CommandResult(
|
|
860
|
+
decision=Decision.ALLOW,
|
|
861
|
+
reason="relationship removed",
|
|
862
|
+
action="remove_relationship",
|
|
863
|
+
object_id=updated.id,
|
|
864
|
+
object_version=updated.version,
|
|
865
|
+
events=[event],
|
|
866
|
+
)
|
|
867
|
+
)
|
|
868
|
+
|
|
869
|
+
def _execute_create(
|
|
870
|
+
self,
|
|
871
|
+
command: CreateObject,
|
|
872
|
+
actor: Actor,
|
|
873
|
+
command_context: CommandContext | None = None,
|
|
874
|
+
) -> CommandResult:
|
|
875
|
+
if command.object_type == 'DelegatedMandate':
|
|
876
|
+
try:
|
|
877
|
+
self.require_delegation()
|
|
878
|
+
except DelegationConfigurationError:
|
|
879
|
+
return self._finalize(CommandResult(
|
|
880
|
+
decision=Decision.DENY, reason='mandate authority unavailable',
|
|
881
|
+
deny_reason=DenyReason.MANDATE_INVALID.value,
|
|
882
|
+
))
|
|
883
|
+
if (not isinstance(command.payload.get('data'), dict)
|
|
884
|
+
or (command.payload.get('state') is not None
|
|
885
|
+
and command.payload['state'] not in ('draft', 'active', 'revoked'))):
|
|
886
|
+
return self._finalize(CommandResult(
|
|
887
|
+
decision=Decision.DENY, reason='mandate authority unavailable',
|
|
888
|
+
deny_reason=DenyReason.MANDATE_INVALID.value,
|
|
889
|
+
))
|
|
890
|
+
type_def = self._types.get(command.object_type or "")
|
|
891
|
+
data = dict(command.payload.get("data", {}))
|
|
892
|
+
canonicalizer = self._extensions.canonicalizer_for(
|
|
893
|
+
command.object_type or "", self._types
|
|
894
|
+
)
|
|
895
|
+
if canonicalizer is not None:
|
|
896
|
+
data = canonicalizer.canonicalize_create(data)
|
|
897
|
+
state = command.payload.get("state") or type_def.default_state()
|
|
898
|
+
relationships = command.payload.get("relationships", [])
|
|
899
|
+
|
|
900
|
+
auth, auth_deny = self._resolve_auth(command_context)
|
|
901
|
+
if auth_deny is not None:
|
|
902
|
+
return auth_deny
|
|
903
|
+
|
|
904
|
+
policy_decision = self._policy.evaluate(
|
|
905
|
+
actor,
|
|
906
|
+
"create",
|
|
907
|
+
None,
|
|
908
|
+
type_def,
|
|
909
|
+
application=auth.application if auth else None,
|
|
910
|
+
mandate=auth.mandate if auth else None,
|
|
911
|
+
resolved_mandate=auth.resolved_mandate if auth else None,
|
|
912
|
+
purpose=auth.purpose if auth else None,
|
|
913
|
+
)
|
|
914
|
+
if policy_decision.decision == Decision.DENY:
|
|
915
|
+
return self._deny(
|
|
916
|
+
command.object_id or "new",
|
|
917
|
+
command.object_type or "",
|
|
918
|
+
actor,
|
|
919
|
+
"create",
|
|
920
|
+
policy_decision.reason,
|
|
921
|
+
None,
|
|
922
|
+
state,
|
|
923
|
+
deny_reason=policy_decision.deny_reason,
|
|
924
|
+
command_context=command_context,
|
|
925
|
+
)
|
|
926
|
+
|
|
927
|
+
if command.object_id and self._store.identities_present([command.object_id]):
|
|
928
|
+
return self._finalize(
|
|
929
|
+
CommandResult(
|
|
930
|
+
decision=Decision.DENY,
|
|
931
|
+
reason="object identity already exists",
|
|
932
|
+
action="create",
|
|
933
|
+
object_id=command.object_id,
|
|
934
|
+
deny_reason=DenyReason.IDENTITY_EXISTS.value,
|
|
935
|
+
)
|
|
936
|
+
)
|
|
937
|
+
|
|
938
|
+
adapter = self._adapters.for_type(command.object_type or "")
|
|
939
|
+
checks: list[ValidationCheck] = []
|
|
940
|
+
if adapter:
|
|
941
|
+
valid, checks, reason = adapter.validate_create(type_def, data)
|
|
942
|
+
if not valid:
|
|
943
|
+
return self._deny(
|
|
944
|
+
command.object_id or "new",
|
|
945
|
+
command.object_type or "",
|
|
946
|
+
actor,
|
|
947
|
+
"create",
|
|
948
|
+
reason,
|
|
949
|
+
None,
|
|
950
|
+
state,
|
|
951
|
+
checks,
|
|
952
|
+
deny_reason=DenyReason.SCHEMA_INVALID,
|
|
953
|
+
)
|
|
954
|
+
else:
|
|
955
|
+
valid, checks, errors = self._schema.validate(type_def, data)
|
|
956
|
+
if not valid:
|
|
957
|
+
return self._deny(
|
|
958
|
+
command.object_id or "new",
|
|
959
|
+
command.object_type or "",
|
|
960
|
+
actor,
|
|
961
|
+
"create",
|
|
962
|
+
"; ".join(errors),
|
|
963
|
+
None,
|
|
964
|
+
state,
|
|
965
|
+
checks,
|
|
966
|
+
deny_reason=DenyReason.SCHEMA_INVALID,
|
|
967
|
+
)
|
|
968
|
+
|
|
969
|
+
# A type with no configured reference is created exactly as before and
|
|
970
|
+
# stores none; only export requires one.
|
|
971
|
+
governance_reference = (
|
|
972
|
+
self._governance.reference_for(type_def.kind, type_def.type_version)
|
|
973
|
+
if self._governance is not None
|
|
974
|
+
else None
|
|
975
|
+
)
|
|
976
|
+
|
|
977
|
+
try:
|
|
978
|
+
canonical_rels = [
|
|
979
|
+
canonical_relationship(
|
|
980
|
+
rel_data.get("source_id", command.object_id or ""),
|
|
981
|
+
rel_data["relation"], rel_data["target_id"], target_type=rel_data.get("target_type"),
|
|
982
|
+
) for rel_data in relationships
|
|
983
|
+
]
|
|
984
|
+
except (KeyError, ValueError, TypeError, AttributeError):
|
|
985
|
+
if command.object_type != 'DelegatedMandate':
|
|
986
|
+
raise
|
|
987
|
+
return self._finalize(CommandResult(
|
|
988
|
+
decision=Decision.DENY, reason='mandate authority unavailable',
|
|
989
|
+
deny_reason=DenyReason.MANDATE_INVALID.value,
|
|
990
|
+
))
|
|
991
|
+
|
|
992
|
+
obj = GovernedObject(
|
|
993
|
+
id=command.object_id or "",
|
|
994
|
+
type=command.object_type or "",
|
|
995
|
+
type_version=type_def.type_version,
|
|
996
|
+
version=1,
|
|
997
|
+
state=state,
|
|
998
|
+
data=data,
|
|
999
|
+
metadata=command.payload.get("metadata", {}),
|
|
1000
|
+
governance_reference=governance_reference,
|
|
1001
|
+
)
|
|
1002
|
+
admission = self._validate_mandate_candidate(obj, canonical_rels, actor, 'create', command_context)
|
|
1003
|
+
if admission is not None:
|
|
1004
|
+
return admission
|
|
1005
|
+
|
|
1006
|
+
rel_details = [
|
|
1007
|
+
{
|
|
1008
|
+
"source_id": r.source_id,
|
|
1009
|
+
"relation": r.relation,
|
|
1010
|
+
"target_id": r.target_id,
|
|
1011
|
+
"target_type": r.target_type,
|
|
1012
|
+
}
|
|
1013
|
+
for r in canonical_rels
|
|
1014
|
+
]
|
|
1015
|
+
create_details: dict[str, Any] = {}
|
|
1016
|
+
if rel_details:
|
|
1017
|
+
create_details["relationships"] = rel_details
|
|
1018
|
+
if governance_reference is not None:
|
|
1019
|
+
create_details["governance_reference"] = governance_reference
|
|
1020
|
+
|
|
1021
|
+
with self._store.observation_transaction():
|
|
1022
|
+
if command.object_id and self._store.identities_present([command.object_id]):
|
|
1023
|
+
return self._finalize(
|
|
1024
|
+
CommandResult(
|
|
1025
|
+
decision=Decision.DENY,
|
|
1026
|
+
reason="object identity already exists",
|
|
1027
|
+
action="create",
|
|
1028
|
+
object_id=command.object_id,
|
|
1029
|
+
deny_reason=DenyReason.IDENTITY_EXISTS.value,
|
|
1030
|
+
)
|
|
1031
|
+
)
|
|
1032
|
+
self._store.save(obj)
|
|
1033
|
+
|
|
1034
|
+
for rel in canonical_rels:
|
|
1035
|
+
self._store.add(rel)
|
|
1036
|
+
|
|
1037
|
+
event = self._allow_event(
|
|
1038
|
+
obj,
|
|
1039
|
+
actor,
|
|
1040
|
+
"create",
|
|
1041
|
+
None,
|
|
1042
|
+
state,
|
|
1043
|
+
"object created",
|
|
1044
|
+
checks,
|
|
1045
|
+
data=data,
|
|
1046
|
+
command_context=command_context,
|
|
1047
|
+
extra_details=create_details or None,
|
|
1048
|
+
)
|
|
1049
|
+
event = self._store.append_accepted(event)
|
|
1050
|
+
return self._finalize(
|
|
1051
|
+
CommandResult(
|
|
1052
|
+
decision=Decision.ALLOW,
|
|
1053
|
+
reason="object created",
|
|
1054
|
+
action="create",
|
|
1055
|
+
object_id=obj.id,
|
|
1056
|
+
new_state=state,
|
|
1057
|
+
object_version=1,
|
|
1058
|
+
events=[event],
|
|
1059
|
+
validation_checks=checks,
|
|
1060
|
+
data=obj.data,
|
|
1061
|
+
)
|
|
1062
|
+
)
|
|
1063
|
+
|
|
1064
|
+
def _execute_read(
|
|
1065
|
+
self,
|
|
1066
|
+
command: ReadObject,
|
|
1067
|
+
actor: Actor,
|
|
1068
|
+
command_context: CommandContext | None = None,
|
|
1069
|
+
) -> CommandResult:
|
|
1070
|
+
# Authority is established before any provider interaction, so a
|
|
1071
|
+
# requester without read authority never observes a provider outcome
|
|
1072
|
+
# (AUTH-ORDER-001, AUTH-DISCLOSE-001).
|
|
1073
|
+
stored = self._store.get(command.object_id or "")
|
|
1074
|
+
if stored is not None:
|
|
1075
|
+
denied = self._read_denial(stored, actor, command_context)
|
|
1076
|
+
if denied is not None:
|
|
1077
|
+
return denied
|
|
1078
|
+
try:
|
|
1079
|
+
obj = self._materializer.materialize(
|
|
1080
|
+
command.object_id or "",
|
|
1081
|
+
self._context,
|
|
1082
|
+
actor,
|
|
1083
|
+
hydrate=True,
|
|
1084
|
+
field_filter=False,
|
|
1085
|
+
persist=True,
|
|
1086
|
+
)
|
|
1087
|
+
except ProviderError as exc:
|
|
1088
|
+
stored = self._store.get(command.object_id or "")
|
|
1089
|
+
return self._provider_failure(
|
|
1090
|
+
command.object_id or "",
|
|
1091
|
+
stored.type if stored else "Unknown",
|
|
1092
|
+
actor,
|
|
1093
|
+
"read",
|
|
1094
|
+
exc,
|
|
1095
|
+
)
|
|
1096
|
+
|
|
1097
|
+
if obj is None:
|
|
1098
|
+
return self._finalize(
|
|
1099
|
+
CommandResult(
|
|
1100
|
+
decision=Decision.DENY,
|
|
1101
|
+
reason="not found",
|
|
1102
|
+
action="read",
|
|
1103
|
+
deny_reason=DenyReason.NOT_FOUND.value,
|
|
1104
|
+
)
|
|
1105
|
+
)
|
|
1106
|
+
|
|
1107
|
+
type_def = self._types.get(obj.type)
|
|
1108
|
+
denied = self._read_denial(obj, actor, command_context)
|
|
1109
|
+
if denied is not None:
|
|
1110
|
+
return denied
|
|
1111
|
+
|
|
1112
|
+
adapter = self._adapters.resolve(obj)
|
|
1113
|
+
fields = command.payload.get("fields") or []
|
|
1114
|
+
data, denied_fields = adapter.filter_read_data(obj, type_def, actor, fields)
|
|
1115
|
+
|
|
1116
|
+
if denied_fields and not data:
|
|
1117
|
+
return self._deny(
|
|
1118
|
+
obj.id,
|
|
1119
|
+
obj.type,
|
|
1120
|
+
actor,
|
|
1121
|
+
"read",
|
|
1122
|
+
"access denied",
|
|
1123
|
+
obj.state,
|
|
1124
|
+
obj.state,
|
|
1125
|
+
)
|
|
1126
|
+
|
|
1127
|
+
reason = "read permitted"
|
|
1128
|
+
if denied_fields:
|
|
1129
|
+
reason = "partial read"
|
|
1130
|
+
|
|
1131
|
+
event = self._allow_event(
|
|
1132
|
+
obj, actor, "read", obj.state, obj.state, reason, command_context=command_context
|
|
1133
|
+
)
|
|
1134
|
+
self._store.append(event)
|
|
1135
|
+
return self._finalize(
|
|
1136
|
+
CommandResult(
|
|
1137
|
+
decision=Decision.ALLOW,
|
|
1138
|
+
reason=reason,
|
|
1139
|
+
action="read",
|
|
1140
|
+
object_id=obj.id,
|
|
1141
|
+
object_version=obj.version,
|
|
1142
|
+
events=[event],
|
|
1143
|
+
data=data,
|
|
1144
|
+
)
|
|
1145
|
+
)
|
|
1146
|
+
|
|
1147
|
+
def _read_not_authorized(self, object_id: str) -> CommandResult:
|
|
1148
|
+
"""Local policy: identical unreadable-target surface, no audit payload."""
|
|
1149
|
+
return self._finalize(CommandResult(
|
|
1150
|
+
decision=Decision.DENY,
|
|
1151
|
+
reason="access denied",
|
|
1152
|
+
action="read",
|
|
1153
|
+
object_id=object_id,
|
|
1154
|
+
deny_reason=DenyReason.READ_NOT_AUTHORIZED.value,
|
|
1155
|
+
))
|
|
1156
|
+
|
|
1157
|
+
def _read_denial(
|
|
1158
|
+
self,
|
|
1159
|
+
obj: GovernedObject,
|
|
1160
|
+
actor: Actor,
|
|
1161
|
+
command_context: CommandContext | None,
|
|
1162
|
+
) -> CommandResult | None:
|
|
1163
|
+
# Resolve independent context errors identically for either binding.
|
|
1164
|
+
auth, auth_deny = self._resolve_auth(command_context)
|
|
1165
|
+
if auth_deny is not None:
|
|
1166
|
+
if auth_deny.deny_reason == DenyReason.MANDATE_INVALID.value:
|
|
1167
|
+
return self._read_not_authorized(obj.id)
|
|
1168
|
+
return auth_deny
|
|
1169
|
+
if not self._types.supports_type_version(obj.type, obj.type_version):
|
|
1170
|
+
return self._read_not_authorized(obj.id)
|
|
1171
|
+
type_def = self._types.get(obj.type)
|
|
1172
|
+
policy_decision = self._policy.evaluate(
|
|
1173
|
+
actor,
|
|
1174
|
+
"read",
|
|
1175
|
+
obj,
|
|
1176
|
+
type_def,
|
|
1177
|
+
application=auth.application if auth else None,
|
|
1178
|
+
mandate=auth.mandate if auth else None,
|
|
1179
|
+
resolved_mandate=auth.resolved_mandate if auth else None,
|
|
1180
|
+
purpose=auth.purpose if auth else None,
|
|
1181
|
+
)
|
|
1182
|
+
if policy_decision.decision == Decision.DENY:
|
|
1183
|
+
return self._read_not_authorized(obj.id)
|
|
1184
|
+
return None
|
|
1185
|
+
|
|
1186
|
+
def _execute_transition(
|
|
1187
|
+
self,
|
|
1188
|
+
command: Command,
|
|
1189
|
+
actor: Actor,
|
|
1190
|
+
command_context: CommandContext | None = None,
|
|
1191
|
+
) -> CommandResult:
|
|
1192
|
+
stored = self._store.get(command.object_id or "")
|
|
1193
|
+
if stored is None:
|
|
1194
|
+
return self._finalize(
|
|
1195
|
+
CommandResult(
|
|
1196
|
+
decision=Decision.DENY,
|
|
1197
|
+
reason="not found",
|
|
1198
|
+
action=command.action,
|
|
1199
|
+
deny_reason=DenyReason.NOT_FOUND.value,
|
|
1200
|
+
)
|
|
1201
|
+
)
|
|
1202
|
+
|
|
1203
|
+
type_def = bound_type_definition(self._types, stored)
|
|
1204
|
+
if type_def is None:
|
|
1205
|
+
return self._read_not_authorized(stored.id).model_copy(update={"action": command.action})
|
|
1206
|
+
action = command.action
|
|
1207
|
+
|
|
1208
|
+
auth, auth_deny = self._resolve_auth(command_context)
|
|
1209
|
+
if auth_deny is not None:
|
|
1210
|
+
return auth_deny
|
|
1211
|
+
|
|
1212
|
+
policy_decision = self._policy.evaluate(
|
|
1213
|
+
actor,
|
|
1214
|
+
action,
|
|
1215
|
+
stored,
|
|
1216
|
+
type_def,
|
|
1217
|
+
application=auth.application if auth else None,
|
|
1218
|
+
mandate=auth.mandate if auth else None,
|
|
1219
|
+
resolved_mandate=auth.resolved_mandate if auth else None,
|
|
1220
|
+
purpose=auth.purpose if auth else None,
|
|
1221
|
+
)
|
|
1222
|
+
if policy_decision.decision == Decision.DENY:
|
|
1223
|
+
return self._deny(
|
|
1224
|
+
stored.id,
|
|
1225
|
+
stored.type,
|
|
1226
|
+
actor,
|
|
1227
|
+
action,
|
|
1228
|
+
policy_decision.reason,
|
|
1229
|
+
stored.state,
|
|
1230
|
+
stored.state,
|
|
1231
|
+
deny_reason=policy_decision.deny_reason,
|
|
1232
|
+
command_context=command_context,
|
|
1233
|
+
)
|
|
1234
|
+
|
|
1235
|
+
candidate = stored.model_copy(update={'state': type_def.transition_target(stored.state, action) or stored.state})
|
|
1236
|
+
admission = self._validate_mandate_candidate(
|
|
1237
|
+
candidate, list_canonical_for_object(self._store, stored.id), actor, action, command_context,
|
|
1238
|
+
)
|
|
1239
|
+
if admission is not None:
|
|
1240
|
+
return admission
|
|
1241
|
+
|
|
1242
|
+
# A cached result is only returned to a requester already authorized
|
|
1243
|
+
# for this operation; it precedes the version check so a retry of an
|
|
1244
|
+
# accepted command still returns its original result.
|
|
1245
|
+
if command_context and command_context.idempotency_key:
|
|
1246
|
+
cached = self._idempotency.get(
|
|
1247
|
+
command.object_id or "", command_context.idempotency_key
|
|
1248
|
+
)
|
|
1249
|
+
if cached is not None:
|
|
1250
|
+
return self._finalize(cached)
|
|
1251
|
+
|
|
1252
|
+
conflict = self._check_version_conflict(
|
|
1253
|
+
stored, action, actor, command_context
|
|
1254
|
+
)
|
|
1255
|
+
if conflict is not None:
|
|
1256
|
+
return conflict
|
|
1257
|
+
|
|
1258
|
+
ctx = AdapterContext(
|
|
1259
|
+
self._store,
|
|
1260
|
+
self._providers,
|
|
1261
|
+
self._types,
|
|
1262
|
+
command_context=command_context,
|
|
1263
|
+
)
|
|
1264
|
+
try:
|
|
1265
|
+
obj = self._materializer.materialize(
|
|
1266
|
+
command.object_id or "",
|
|
1267
|
+
ctx,
|
|
1268
|
+
hydrate=True,
|
|
1269
|
+
persist=False,
|
|
1270
|
+
)
|
|
1271
|
+
except ProviderError as exc:
|
|
1272
|
+
return self._provider_failure(
|
|
1273
|
+
command.object_id or "",
|
|
1274
|
+
stored.type,
|
|
1275
|
+
actor,
|
|
1276
|
+
command.action,
|
|
1277
|
+
exc,
|
|
1278
|
+
)
|
|
1279
|
+
|
|
1280
|
+
if obj is None:
|
|
1281
|
+
return self._finalize(
|
|
1282
|
+
CommandResult(
|
|
1283
|
+
decision=Decision.DENY,
|
|
1284
|
+
reason="not found",
|
|
1285
|
+
action=command.action,
|
|
1286
|
+
deny_reason=DenyReason.NOT_FOUND.value,
|
|
1287
|
+
)
|
|
1288
|
+
)
|
|
1289
|
+
|
|
1290
|
+
ok, lifecycle_reason, lifecycle_check = self._lifecycle.validate_transition(
|
|
1291
|
+
type_def, stored.state, action
|
|
1292
|
+
)
|
|
1293
|
+
checks = [lifecycle_check]
|
|
1294
|
+
if not ok:
|
|
1295
|
+
return self._deny(
|
|
1296
|
+
obj.id,
|
|
1297
|
+
obj.type,
|
|
1298
|
+
actor,
|
|
1299
|
+
action,
|
|
1300
|
+
lifecycle_reason,
|
|
1301
|
+
obj.state,
|
|
1302
|
+
obj.state,
|
|
1303
|
+
checks,
|
|
1304
|
+
deny_reason=DenyReason.LIFECYCLE_INVALID,
|
|
1305
|
+
command_context=command_context,
|
|
1306
|
+
)
|
|
1307
|
+
|
|
1308
|
+
adapter = self._adapters.resolve(obj)
|
|
1309
|
+
domain_ok, domain_checks, domain_reason = adapter.validate_transition(
|
|
1310
|
+
obj, type_def, action, actor
|
|
1311
|
+
)
|
|
1312
|
+
checks.extend(domain_checks)
|
|
1313
|
+
if not domain_ok:
|
|
1314
|
+
return self._deny(
|
|
1315
|
+
obj.id,
|
|
1316
|
+
obj.type,
|
|
1317
|
+
actor,
|
|
1318
|
+
action,
|
|
1319
|
+
domain_reason,
|
|
1320
|
+
obj.state,
|
|
1321
|
+
obj.state,
|
|
1322
|
+
checks,
|
|
1323
|
+
deny_reason=DenyReason.OBJECT_POLICY_DENIED,
|
|
1324
|
+
command_context=command_context,
|
|
1325
|
+
)
|
|
1326
|
+
|
|
1327
|
+
new_version = self._allocate_version(obj)
|
|
1328
|
+
if new_version is None:
|
|
1329
|
+
return self._version_space_exhausted(obj, actor, action, command_context)
|
|
1330
|
+
previous_state = obj.state
|
|
1331
|
+
adapter_result = adapter.adapter_execute(obj, command, ctx)
|
|
1332
|
+
|
|
1333
|
+
if adapter_result is not None:
|
|
1334
|
+
if not adapter_result.success:
|
|
1335
|
+
if adapter_result.failure == AdapterFailure.INVALID:
|
|
1336
|
+
return self._adapter_rejected(
|
|
1337
|
+
obj, actor, action, adapter_result, checks, command_context
|
|
1338
|
+
)
|
|
1339
|
+
if adapter_result.failure == AdapterFailure.PROVIDER:
|
|
1340
|
+
err = ProviderError(
|
|
1341
|
+
adapter_result.error_message or "provider failed",
|
|
1342
|
+
code=adapter_result.error_code or ProviderError.code,
|
|
1343
|
+
)
|
|
1344
|
+
return self._provider_failure(
|
|
1345
|
+
obj.id, obj.type, actor, action, err, checks=checks
|
|
1346
|
+
)
|
|
1347
|
+
return self._adapter_result_invalid(
|
|
1348
|
+
obj, actor, action, "adapter failure has no failure classification",
|
|
1349
|
+
checks, command_context, adapter_result=adapter_result,
|
|
1350
|
+
)
|
|
1351
|
+
if adapter_result.failure is not None:
|
|
1352
|
+
return self._adapter_result_invalid(
|
|
1353
|
+
obj, actor, action, "adapter reported success with a failure classification",
|
|
1354
|
+
checks, command_context, adapter_result=adapter_result,
|
|
1355
|
+
)
|
|
1356
|
+
proposed = adapter_result.object
|
|
1357
|
+
if proposed is None:
|
|
1358
|
+
return self._adapter_result_invalid(
|
|
1359
|
+
obj, actor, action, "adapter reported success without a resulting object",
|
|
1360
|
+
checks, command_context,
|
|
1361
|
+
)
|
|
1362
|
+
# The runtime alone issues Object Versions; any adapter-proposed
|
|
1363
|
+
# version is discarded. An accepted transition is a revision even
|
|
1364
|
+
# when its resulting values equal the prior ones.
|
|
1365
|
+
obj = proposed.model_copy(update={"version": new_version})
|
|
1366
|
+
if adapter_result.relationships:
|
|
1367
|
+
binding = obj.metadata.get("provider_binding", {})
|
|
1368
|
+
pname = binding.get("provider_name") or binding.get("provider")
|
|
1369
|
+
if pname:
|
|
1370
|
+
self._rel_sync.sync_provider_relationships(
|
|
1371
|
+
obj.id, adapter_result.relationships, provider_name=pname
|
|
1372
|
+
)
|
|
1373
|
+
if command_context and command_context.correlation_id:
|
|
1374
|
+
obj = self._observations.record_governed_echo(
|
|
1375
|
+
obj,
|
|
1376
|
+
correlation_id=command_context.correlation_id,
|
|
1377
|
+
)
|
|
1378
|
+
new_state = obj.state
|
|
1379
|
+
else:
|
|
1380
|
+
new_state = type_def.transition_target(obj.state, action)
|
|
1381
|
+
if new_state is None:
|
|
1382
|
+
return self._deny(
|
|
1383
|
+
obj.id,
|
|
1384
|
+
obj.type,
|
|
1385
|
+
actor,
|
|
1386
|
+
action,
|
|
1387
|
+
f"no transition for {action} from {obj.state}",
|
|
1388
|
+
obj.state,
|
|
1389
|
+
obj.state,
|
|
1390
|
+
checks,
|
|
1391
|
+
)
|
|
1392
|
+
obj = obj.model_copy(update={"state": new_state, "version": new_version})
|
|
1393
|
+
|
|
1394
|
+
with self._store.observation_transaction():
|
|
1395
|
+
current = self._store.get(obj.id)
|
|
1396
|
+
if current is None:
|
|
1397
|
+
return self._finalize(
|
|
1398
|
+
CommandResult(
|
|
1399
|
+
decision=Decision.DENY,
|
|
1400
|
+
reason="not found",
|
|
1401
|
+
action=action,
|
|
1402
|
+
deny_reason=DenyReason.NOT_FOUND.value,
|
|
1403
|
+
)
|
|
1404
|
+
)
|
|
1405
|
+
if current.version != stored.version:
|
|
1406
|
+
return self._conflict(
|
|
1407
|
+
current,
|
|
1408
|
+
actor,
|
|
1409
|
+
action,
|
|
1410
|
+
expected_version=stored.version,
|
|
1411
|
+
actual_version=current.version,
|
|
1412
|
+
command_context=command_context,
|
|
1413
|
+
)
|
|
1414
|
+
self._store.save(obj)
|
|
1415
|
+
|
|
1416
|
+
event = self._allow_event(
|
|
1417
|
+
obj,
|
|
1418
|
+
actor,
|
|
1419
|
+
action,
|
|
1420
|
+
previous_state,
|
|
1421
|
+
new_state,
|
|
1422
|
+
"transition applied",
|
|
1423
|
+
checks,
|
|
1424
|
+
command_context=command_context,
|
|
1425
|
+
extra_details={
|
|
1426
|
+
"accepted_post_image": {"data": dict(obj.data), "state": new_state}
|
|
1427
|
+
},
|
|
1428
|
+
)
|
|
1429
|
+
event = self._store.append_accepted(event)
|
|
1430
|
+
result = self._finalize(
|
|
1431
|
+
CommandResult(
|
|
1432
|
+
decision=Decision.ALLOW,
|
|
1433
|
+
reason="transition applied",
|
|
1434
|
+
action=action,
|
|
1435
|
+
object_id=obj.id,
|
|
1436
|
+
previous_state=previous_state,
|
|
1437
|
+
new_state=new_state,
|
|
1438
|
+
object_version=obj.version,
|
|
1439
|
+
events=[event],
|
|
1440
|
+
validation_checks=checks,
|
|
1441
|
+
data=obj.data,
|
|
1442
|
+
)
|
|
1443
|
+
)
|
|
1444
|
+
if command_context and command_context.idempotency_key:
|
|
1445
|
+
self._idempotency.put(
|
|
1446
|
+
obj.id,
|
|
1447
|
+
command_context.idempotency_key,
|
|
1448
|
+
result,
|
|
1449
|
+
command_id=command_context.command_id,
|
|
1450
|
+
correlation_id=command_context.correlation_id,
|
|
1451
|
+
)
|
|
1452
|
+
return result
|
|
1453
|
+
|
|
1454
|
+
def require_delegation(self):
|
|
1455
|
+
if self._delegation is None:
|
|
1456
|
+
raise DelegationConfigurationError()
|
|
1457
|
+
self._delegation.validate(self._types, self._adapters, self._extensions)
|
|
1458
|
+
return self._delegation
|
|
1459
|
+
|
|
1460
|
+
def _decode_legacy_scope(self, scoped_to, store):
|
|
1461
|
+
return self.require_delegation().scope_decoder(scoped_to, store)
|
|
1462
|
+
|
|
1463
|
+
def _validate_delegation_binding(self, obj):
|
|
1464
|
+
configuration = self.require_delegation()
|
|
1465
|
+
configuration.validate(self._types, self._adapters, self._extensions, obj)
|
|
1466
|
+
mandate = materialize_mandate(obj, self._store, scope_decoder=configuration.scope_decoder)
|
|
1467
|
+
configuration.actor(mandate.principal_id)
|
|
1468
|
+
configuration.actor(mandate.delegate_actor_id)
|
|
1469
|
+
|
|
1470
|
+
def _validate_mandate_candidate(self, obj, relationships, actor, action, command_context):
|
|
1471
|
+
if obj.type != 'DelegatedMandate':
|
|
1472
|
+
return None
|
|
1473
|
+
try:
|
|
1474
|
+
configuration = self.require_delegation()
|
|
1475
|
+
configuration.validate(self._types, self._adapters, self._extensions, obj)
|
|
1476
|
+
mandate = materialize_mandate(obj, self._store, scope_decoder=configuration.scope_decoder,
|
|
1477
|
+
relationships=relationships)
|
|
1478
|
+
principal = configuration.actor(mandate.principal_id)
|
|
1479
|
+
configuration.actor(mandate.delegate_actor_id)
|
|
1480
|
+
if action != 'revoke':
|
|
1481
|
+
parent = None
|
|
1482
|
+
ancestors = frozenset()
|
|
1483
|
+
if mandate.parent_mandate_id:
|
|
1484
|
+
if mandate.parent_mandate_id == obj.id:
|
|
1485
|
+
raise ValueError('delegation cycle')
|
|
1486
|
+
parent = self._mandate_resolver.resolve_authority(mandate.parent_mandate_id)
|
|
1487
|
+
if parent is None or not parent.is_effective_at(self._clock.now())[0]:
|
|
1488
|
+
raise ValueError('ineffective parent')
|
|
1489
|
+
ancestors = frozenset(parent.parent_mandate_ids)
|
|
1490
|
+
if obj.id in ancestors:
|
|
1491
|
+
raise ValueError('delegation cycle')
|
|
1492
|
+
validation = self._delegation_validator.validate(
|
|
1493
|
+
principal=principal,
|
|
1494
|
+
spec=DelegationSpec(mandate.capabilities, mandate.scope, mandate.purposes,
|
|
1495
|
+
mandate.valid_from, mandate.valid_until, mandate.parent_mandate_id, mandate.id),
|
|
1496
|
+
parent_mandate=parent, ancestor_ids=ancestors,
|
|
1497
|
+
max_depth=self._mandate_resolver.MAX_CHAIN_DEPTH+1,
|
|
1498
|
+
)
|
|
1499
|
+
if not validation.valid:
|
|
1500
|
+
raise ValueError('invalid delegation')
|
|
1501
|
+
except (ValueError, TypeError, KeyError, AttributeError):
|
|
1502
|
+
return self._deny(obj.id, obj.type, actor, action, 'mandate authority unavailable',
|
|
1503
|
+
obj.state, obj.state, deny_reason=DenyReason.MANDATE_INVALID,
|
|
1504
|
+
command_context=command_context)
|
|
1505
|
+
return None
|
|
1506
|
+
|
|
1507
|
+
def resolve_authorization_context(
|
|
1508
|
+
self, auth: AuthorizationContext | None
|
|
1509
|
+
) -> AuthorizationContext | None:
|
|
1510
|
+
"""Re-resolve referenced bounds; snapshots are identities, not grants."""
|
|
1511
|
+
if auth is None:
|
|
1512
|
+
return None
|
|
1513
|
+
if (not isinstance(auth, AuthorizationContext)
|
|
1514
|
+
or (auth.application is not None and not isinstance(auth.application, ApplicationDefinition))
|
|
1515
|
+
or (auth.mandate is not None and not isinstance(auth.mandate, DelegatedMandate))
|
|
1516
|
+
or (auth.resolved_mandate is not None and not isinstance(auth.resolved_mandate, ResolvedMandateAuthority))):
|
|
1517
|
+
raise AuthorizationContextError()
|
|
1518
|
+
app_values = (auth.application_id, auth.application.id if auth.application else None)
|
|
1519
|
+
mandate_values = (
|
|
1520
|
+
auth.mandate_id, auth.mandate.id if auth.mandate else None,
|
|
1521
|
+
auth.resolved_mandate.mandate_id if auth.resolved_mandate else None,
|
|
1522
|
+
)
|
|
1523
|
+
if any(value is not None and (not isinstance(value, str) or not value)
|
|
1524
|
+
for value in (*app_values, *mandate_values)):
|
|
1525
|
+
raise AuthorizationContextError()
|
|
1526
|
+
app_ids = {value for value in app_values if value is not None}
|
|
1527
|
+
mandate_ids = {value for value in mandate_values if value is not None}
|
|
1528
|
+
if len(app_ids) > 1 or len(mandate_ids) > 1:
|
|
1529
|
+
raise AuthorizationContextError()
|
|
1530
|
+
if auth.purpose is not None and (
|
|
1531
|
+
not isinstance(auth.purpose, str) or not auth.purpose
|
|
1532
|
+
):
|
|
1533
|
+
raise AuthorizationContextError()
|
|
1534
|
+
current, deny = self._resolve_auth(CommandContext(
|
|
1535
|
+
application_id=next(iter(app_ids), None),
|
|
1536
|
+
mandate_id=next(iter(mandate_ids), None), purpose=auth.purpose,
|
|
1537
|
+
))
|
|
1538
|
+
if deny is not None:
|
|
1539
|
+
raise AuthorizationContextError()
|
|
1540
|
+
return current
|
|
1541
|
+
|
|
1542
|
+
def _resolve_auth(
|
|
1543
|
+
self, command_context: CommandContext | None
|
|
1544
|
+
) -> tuple[AuthorizationContext | None, CommandResult | None]:
|
|
1545
|
+
if command_context is not None:
|
|
1546
|
+
for value, reason in ((command_context.application_id, DenyReason.APPLICATION_UNKNOWN),
|
|
1547
|
+
(command_context.mandate_id, DenyReason.MANDATE_INVALID),
|
|
1548
|
+
(command_context.purpose, DenyReason.MANDATE_INVALID)):
|
|
1549
|
+
if value is not None and (not isinstance(value, str) or not value):
|
|
1550
|
+
return None, self._finalize(CommandResult(
|
|
1551
|
+
decision=Decision.DENY, reason='authorization context unavailable', deny_reason=reason.value,
|
|
1552
|
+
))
|
|
1553
|
+
app, app_deny = self._resolve_application(command_context)
|
|
1554
|
+
if app_deny is not None:
|
|
1555
|
+
return None, app_deny
|
|
1556
|
+
mandate, resolved, mandate_deny = self._resolve_mandate(command_context)
|
|
1557
|
+
if mandate_deny is not None:
|
|
1558
|
+
return None, mandate_deny
|
|
1559
|
+
purpose = command_context.purpose if command_context else None
|
|
1560
|
+
if app is None and mandate is None and purpose is None:
|
|
1561
|
+
return None, None
|
|
1562
|
+
return (
|
|
1563
|
+
AuthorizationContext(
|
|
1564
|
+
application=app,
|
|
1565
|
+
mandate=mandate,
|
|
1566
|
+
resolved_mandate=resolved,
|
|
1567
|
+
purpose=purpose,
|
|
1568
|
+
application_id=command_context.application_id if command_context else None,
|
|
1569
|
+
mandate_id=command_context.mandate_id if command_context else None,
|
|
1570
|
+
),
|
|
1571
|
+
None,
|
|
1572
|
+
)
|
|
1573
|
+
|
|
1574
|
+
def _resolve_mandate(
|
|
1575
|
+
self, command_context: CommandContext | None
|
|
1576
|
+
) -> tuple[Any | None, Any | None, CommandResult | None]:
|
|
1577
|
+
if command_context is None or not command_context.mandate_id:
|
|
1578
|
+
return None, None, None
|
|
1579
|
+
try:
|
|
1580
|
+
resolved = self._mandate_resolver.resolve_authority(command_context.mandate_id)
|
|
1581
|
+
mandate = self._mandate_resolver.resolve(command_context.mandate_id) if resolved else None
|
|
1582
|
+
except (ValueError, TypeError, KeyError, AttributeError):
|
|
1583
|
+
return None, None, self._finalize(CommandResult(
|
|
1584
|
+
decision=Decision.DENY, reason='mandate authority unavailable',
|
|
1585
|
+
deny_reason=DenyReason.MANDATE_INVALID.value,
|
|
1586
|
+
))
|
|
1587
|
+
if resolved is None:
|
|
1588
|
+
return None, None, self._finalize(
|
|
1589
|
+
CommandResult(
|
|
1590
|
+
decision=Decision.DENY,
|
|
1591
|
+
reason=f"unknown mandate: {command_context.mandate_id}",
|
|
1592
|
+
deny_reason=DenyReason.MANDATE_UNKNOWN.value,
|
|
1593
|
+
)
|
|
1594
|
+
)
|
|
1595
|
+
return mandate, resolved, None
|
|
1596
|
+
|
|
1597
|
+
def _resolve_application(
|
|
1598
|
+
self, command_context: CommandContext | None
|
|
1599
|
+
) -> tuple[ApplicationDefinition | None, CommandResult | None]:
|
|
1600
|
+
if command_context is None or not command_context.application_id:
|
|
1601
|
+
return None, None
|
|
1602
|
+
app = self._applications.get(command_context.application_id)
|
|
1603
|
+
if app is None:
|
|
1604
|
+
return None, self._finalize(
|
|
1605
|
+
CommandResult(
|
|
1606
|
+
decision=Decision.DENY,
|
|
1607
|
+
reason=f"unknown application: {command_context.application_id}",
|
|
1608
|
+
deny_reason=DenyReason.APPLICATION_UNKNOWN.value,
|
|
1609
|
+
)
|
|
1610
|
+
)
|
|
1611
|
+
return app, None
|
|
1612
|
+
|
|
1613
|
+
def _allocate_version(self, obj: GovernedObject) -> int | None:
|
|
1614
|
+
"""Next unused Object Version, or None when the version space is exhausted."""
|
|
1615
|
+
return self._store.next_object_version(obj)
|
|
1616
|
+
|
|
1617
|
+
def _version_space_exhausted(
|
|
1618
|
+
self,
|
|
1619
|
+
obj: GovernedObject,
|
|
1620
|
+
actor: Actor,
|
|
1621
|
+
action: str,
|
|
1622
|
+
command_context: CommandContext | None,
|
|
1623
|
+
) -> CommandResult:
|
|
1624
|
+
result = self._deny(
|
|
1625
|
+
obj.id,
|
|
1626
|
+
obj.type,
|
|
1627
|
+
actor,
|
|
1628
|
+
action,
|
|
1629
|
+
"object version space exhausted",
|
|
1630
|
+
obj.state,
|
|
1631
|
+
obj.state,
|
|
1632
|
+
deny_reason=DenyReason.OBJECT_VERSION_SPACE_EXHAUSTED,
|
|
1633
|
+
command_context=command_context,
|
|
1634
|
+
)
|
|
1635
|
+
return result.model_copy(update={"outcome_category": REFUSED})
|
|
1636
|
+
|
|
1637
|
+
def _adapter_result_invalid(
|
|
1638
|
+
self,
|
|
1639
|
+
obj: GovernedObject,
|
|
1640
|
+
actor: Actor,
|
|
1641
|
+
action: str,
|
|
1642
|
+
reason: str,
|
|
1643
|
+
checks: list[ValidationCheck],
|
|
1644
|
+
command_context: CommandContext | None,
|
|
1645
|
+
*,
|
|
1646
|
+
adapter_result: AdapterExecuteResult | None = None,
|
|
1647
|
+
) -> CommandResult:
|
|
1648
|
+
"""The adapter broke its execute contract; nothing canonical is accepted."""
|
|
1649
|
+
extra = None
|
|
1650
|
+
if adapter_result is not None:
|
|
1651
|
+
extra = {
|
|
1652
|
+
"adapter_error": adapter_result.error_code,
|
|
1653
|
+
"message": adapter_result.error_message,
|
|
1654
|
+
"adapter_success": adapter_result.success,
|
|
1655
|
+
"adapter_failure": adapter_result.failure,
|
|
1656
|
+
}
|
|
1657
|
+
result = self._deny(
|
|
1658
|
+
obj.id,
|
|
1659
|
+
obj.type,
|
|
1660
|
+
actor,
|
|
1661
|
+
action,
|
|
1662
|
+
reason,
|
|
1663
|
+
obj.state,
|
|
1664
|
+
obj.state,
|
|
1665
|
+
checks,
|
|
1666
|
+
deny_reason=DenyReason.ADAPTER_RESULT_INVALID,
|
|
1667
|
+
command_context=command_context,
|
|
1668
|
+
extra_details=extra,
|
|
1669
|
+
)
|
|
1670
|
+
return result.model_copy(update={"outcome_category": REFUSED})
|
|
1671
|
+
|
|
1672
|
+
def _check_version_conflict(
|
|
1673
|
+
self,
|
|
1674
|
+
stored: GovernedObject,
|
|
1675
|
+
action: str,
|
|
1676
|
+
actor: Actor,
|
|
1677
|
+
command_context: CommandContext | None,
|
|
1678
|
+
) -> CommandResult | None:
|
|
1679
|
+
if command_context is None or command_context.expected_version is None:
|
|
1680
|
+
return None
|
|
1681
|
+
if stored.version == command_context.expected_version:
|
|
1682
|
+
return None
|
|
1683
|
+
return self._conflict(
|
|
1684
|
+
stored,
|
|
1685
|
+
actor,
|
|
1686
|
+
action,
|
|
1687
|
+
expected_version=command_context.expected_version,
|
|
1688
|
+
actual_version=stored.version,
|
|
1689
|
+
command_context=command_context,
|
|
1690
|
+
)
|
|
1691
|
+
|
|
1692
|
+
def _conflict(
|
|
1693
|
+
self,
|
|
1694
|
+
obj: GovernedObject,
|
|
1695
|
+
actor: Actor,
|
|
1696
|
+
action: str,
|
|
1697
|
+
*,
|
|
1698
|
+
expected_version: int,
|
|
1699
|
+
actual_version: int,
|
|
1700
|
+
command_context: CommandContext | None = None,
|
|
1701
|
+
) -> CommandResult:
|
|
1702
|
+
details: dict = {
|
|
1703
|
+
"expected_version": expected_version,
|
|
1704
|
+
"actual_version": actual_version,
|
|
1705
|
+
"conflict_reason": ConflictReason.VERSION_MISMATCH.value,
|
|
1706
|
+
}
|
|
1707
|
+
if command_context is not None:
|
|
1708
|
+
meta = self._context_metadata(command_context)
|
|
1709
|
+
if meta is not None:
|
|
1710
|
+
details["command_context"] = meta
|
|
1711
|
+
event = GovernanceEvent(
|
|
1712
|
+
object_id=obj.id,
|
|
1713
|
+
object_type=obj.type,
|
|
1714
|
+
actor_id=actor.id,
|
|
1715
|
+
actor_type=actor.type.value,
|
|
1716
|
+
action=action,
|
|
1717
|
+
decision=Decision.CONFLICT,
|
|
1718
|
+
event_type=f"object.{action}.conflict",
|
|
1719
|
+
previous_state=obj.state,
|
|
1720
|
+
new_state=obj.state,
|
|
1721
|
+
reason="version conflict",
|
|
1722
|
+
object_version=actual_version,
|
|
1723
|
+
details=details,
|
|
1724
|
+
)
|
|
1725
|
+
self._store.append(event)
|
|
1726
|
+
return self._finalize(
|
|
1727
|
+
CommandResult(
|
|
1728
|
+
decision=Decision.CONFLICT,
|
|
1729
|
+
reason="version conflict",
|
|
1730
|
+
conflict_reason=ConflictReason.VERSION_MISMATCH.value,
|
|
1731
|
+
expected_version=expected_version,
|
|
1732
|
+
actual_version=actual_version,
|
|
1733
|
+
action=action,
|
|
1734
|
+
object_id=obj.id,
|
|
1735
|
+
previous_state=obj.state,
|
|
1736
|
+
new_state=obj.state,
|
|
1737
|
+
object_version=actual_version,
|
|
1738
|
+
events=[event],
|
|
1739
|
+
)
|
|
1740
|
+
)
|
|
1741
|
+
|
|
1742
|
+
def _context_metadata(self, command_context: CommandContext | None) -> dict | None:
|
|
1743
|
+
if command_context is None:
|
|
1744
|
+
return None
|
|
1745
|
+
meta = command_context.to_metadata()
|
|
1746
|
+
if command_context.mandate_id:
|
|
1747
|
+
mandate = self._mandate_resolver.resolve(command_context.mandate_id)
|
|
1748
|
+
if mandate is not None:
|
|
1749
|
+
meta["principal_id"] = mandate.principal_id
|
|
1750
|
+
return meta
|
|
1751
|
+
|
|
1752
|
+
def _provider_failure(
|
|
1753
|
+
self,
|
|
1754
|
+
object_id: str,
|
|
1755
|
+
object_type: str,
|
|
1756
|
+
actor: Actor,
|
|
1757
|
+
action: str,
|
|
1758
|
+
exc: ProviderError,
|
|
1759
|
+
checks: list[ValidationCheck] | None = None,
|
|
1760
|
+
) -> CommandResult:
|
|
1761
|
+
"""The backing provider failed on an otherwise evaluable request.
|
|
1762
|
+
|
|
1763
|
+
No minimum outcome class describes this condition, so it is REFUSED
|
|
1764
|
+
(OP-OUTCOME-001); nothing canonical changes (OP-NOCHANGE-001)."""
|
|
1765
|
+
obj = self._store.get(object_id)
|
|
1766
|
+
version = obj.version if obj else 1
|
|
1767
|
+
state = obj.state if obj else None
|
|
1768
|
+
reason = exc.code
|
|
1769
|
+
events: list[GovernanceEvent] = []
|
|
1770
|
+
if action != "read":
|
|
1771
|
+
event = GovernanceEvent(
|
|
1772
|
+
object_id=object_id,
|
|
1773
|
+
object_type=object_type,
|
|
1774
|
+
actor_id=actor.id,
|
|
1775
|
+
actor_type=actor.type.value,
|
|
1776
|
+
action=action,
|
|
1777
|
+
decision=Decision.DENY,
|
|
1778
|
+
event_type=f"object.{action}.provider_failed",
|
|
1779
|
+
previous_state=state,
|
|
1780
|
+
new_state=state,
|
|
1781
|
+
reason=reason,
|
|
1782
|
+
object_version=version,
|
|
1783
|
+
details={
|
|
1784
|
+
"provider_error": exc.code,
|
|
1785
|
+
"message": exc.message,
|
|
1786
|
+
"deny_reason": DenyReason.PROVIDER_FAILED.value,
|
|
1787
|
+
},
|
|
1788
|
+
)
|
|
1789
|
+
self._store.append(event)
|
|
1790
|
+
events = [event]
|
|
1791
|
+
return self._finalize(
|
|
1792
|
+
CommandResult(
|
|
1793
|
+
decision=Decision.DENY,
|
|
1794
|
+
reason=reason,
|
|
1795
|
+
deny_reason=DenyReason.PROVIDER_FAILED.value,
|
|
1796
|
+
action=action,
|
|
1797
|
+
object_id=object_id,
|
|
1798
|
+
events=events,
|
|
1799
|
+
validation_checks=checks or [],
|
|
1800
|
+
)
|
|
1801
|
+
)
|
|
1802
|
+
|
|
1803
|
+
def _adapter_rejected(
|
|
1804
|
+
self,
|
|
1805
|
+
obj: GovernedObject,
|
|
1806
|
+
actor: Actor,
|
|
1807
|
+
action: str,
|
|
1808
|
+
result: AdapterExecuteResult,
|
|
1809
|
+
checks: list[ValidationCheck],
|
|
1810
|
+
command_context: CommandContext | None,
|
|
1811
|
+
) -> CommandResult:
|
|
1812
|
+
"""The type's adapter rejected the requested change under the type's
|
|
1813
|
+
declared rules: SEMANTICALLY_INVALID (TYPE-VALID-001)."""
|
|
1814
|
+
message = result.error_message or "change rejected by type rules"
|
|
1815
|
+
return self._deny(
|
|
1816
|
+
obj.id,
|
|
1817
|
+
obj.type,
|
|
1818
|
+
actor,
|
|
1819
|
+
action,
|
|
1820
|
+
result.error_code or message,
|
|
1821
|
+
obj.state,
|
|
1822
|
+
obj.state,
|
|
1823
|
+
[*checks, ValidationCheck(name="type_state", passed=False, message=message)],
|
|
1824
|
+
deny_reason=DenyReason.STATE_INVALID,
|
|
1825
|
+
command_context=command_context,
|
|
1826
|
+
extra_details={"adapter_error": result.error_code, "message": message},
|
|
1827
|
+
)
|
|
1828
|
+
|
|
1829
|
+
def _deny(
|
|
1830
|
+
self,
|
|
1831
|
+
object_id: str,
|
|
1832
|
+
object_type: str,
|
|
1833
|
+
actor: Actor,
|
|
1834
|
+
action: str,
|
|
1835
|
+
reason: str,
|
|
1836
|
+
previous_state: str | None,
|
|
1837
|
+
new_state: str | None,
|
|
1838
|
+
checks: list[ValidationCheck] | None = None,
|
|
1839
|
+
*,
|
|
1840
|
+
deny_reason: DenyReason | None = None,
|
|
1841
|
+
command_context: CommandContext | None = None,
|
|
1842
|
+
extra_details: dict | None = None,
|
|
1843
|
+
) -> CommandResult:
|
|
1844
|
+
obj = self._store.get(object_id)
|
|
1845
|
+
version = obj.version if obj else 1
|
|
1846
|
+
details: dict = dict(extra_details or {})
|
|
1847
|
+
if deny_reason is not None:
|
|
1848
|
+
details["deny_reason"] = deny_reason.value
|
|
1849
|
+
if command_context is not None:
|
|
1850
|
+
meta = self._context_metadata(command_context)
|
|
1851
|
+
if meta is not None:
|
|
1852
|
+
details["command_context"] = meta
|
|
1853
|
+
event = GovernanceEvent(
|
|
1854
|
+
object_id=object_id,
|
|
1855
|
+
object_type=object_type,
|
|
1856
|
+
actor_id=actor.id,
|
|
1857
|
+
actor_type=actor.type.value,
|
|
1858
|
+
action=action,
|
|
1859
|
+
decision=Decision.DENY,
|
|
1860
|
+
event_type=f"object.{action}.denied",
|
|
1861
|
+
previous_state=previous_state,
|
|
1862
|
+
new_state=new_state or previous_state,
|
|
1863
|
+
reason=reason,
|
|
1864
|
+
object_version=version,
|
|
1865
|
+
details=details,
|
|
1866
|
+
)
|
|
1867
|
+
self._store.append(event)
|
|
1868
|
+
return self._finalize(
|
|
1869
|
+
CommandResult(
|
|
1870
|
+
decision=Decision.DENY,
|
|
1871
|
+
reason=reason,
|
|
1872
|
+
deny_reason=deny_reason.value if deny_reason else None,
|
|
1873
|
+
action=action,
|
|
1874
|
+
object_id=object_id,
|
|
1875
|
+
previous_state=previous_state,
|
|
1876
|
+
new_state=new_state or previous_state,
|
|
1877
|
+
object_version=version,
|
|
1878
|
+
events=[event],
|
|
1879
|
+
validation_checks=checks or [],
|
|
1880
|
+
)
|
|
1881
|
+
)
|
|
1882
|
+
|
|
1883
|
+
def _allow_event(
|
|
1884
|
+
self,
|
|
1885
|
+
obj: GovernedObject,
|
|
1886
|
+
actor: Actor,
|
|
1887
|
+
action: str,
|
|
1888
|
+
previous_state: str | None,
|
|
1889
|
+
new_state: str | None,
|
|
1890
|
+
reason: str,
|
|
1891
|
+
checks: list[ValidationCheck] | None = None,
|
|
1892
|
+
*,
|
|
1893
|
+
data: dict | None = None,
|
|
1894
|
+
command_context: CommandContext | None = None,
|
|
1895
|
+
extra_details: dict | None = None,
|
|
1896
|
+
) -> GovernanceEvent:
|
|
1897
|
+
details: dict = {"validation_checks": [c.model_dump() for c in (checks or [])]}
|
|
1898
|
+
if data is not None:
|
|
1899
|
+
details["data"] = data
|
|
1900
|
+
if extra_details:
|
|
1901
|
+
details.update(extra_details)
|
|
1902
|
+
meta = self._context_metadata(command_context)
|
|
1903
|
+
if meta is not None:
|
|
1904
|
+
details["command_context"] = meta
|
|
1905
|
+
return GovernanceEvent(
|
|
1906
|
+
object_id=obj.id,
|
|
1907
|
+
object_type=obj.type,
|
|
1908
|
+
type_version=obj.type_version,
|
|
1909
|
+
actor_id=actor.id,
|
|
1910
|
+
actor_type=actor.type.value,
|
|
1911
|
+
action=action,
|
|
1912
|
+
decision=Decision.ALLOW,
|
|
1913
|
+
event_type=f"object.{action}",
|
|
1914
|
+
previous_state=previous_state,
|
|
1915
|
+
new_state=new_state,
|
|
1916
|
+
reason=reason,
|
|
1917
|
+
object_version=obj.version,
|
|
1918
|
+
details=details,
|
|
1919
|
+
)
|