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,38 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# Copyright 2026 Brafford Labs, LLC
|
|
3
|
+
"""Single version source: ``pyproject.toml`` (``[project].version``).
|
|
4
|
+
|
|
5
|
+
An installed distribution reports it through package metadata. When the package is
|
|
6
|
+
imported from a source checkout (for example ``PYTHONPATH=src``), the version is read
|
|
7
|
+
from the adjacent ``pyproject.toml`` instead. Nothing else defines a version.
|
|
8
|
+
"""
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from importlib import metadata
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
|
|
14
|
+
DISTRIBUTION_NAME = "governed-objects"
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _from_source_checkout() -> str | None:
|
|
18
|
+
import tomllib
|
|
19
|
+
|
|
20
|
+
pyproject = Path(__file__).resolve().parents[2] / "pyproject.toml"
|
|
21
|
+
try:
|
|
22
|
+
project = tomllib.loads(pyproject.read_text(encoding="utf-8")).get("project", {})
|
|
23
|
+
except (OSError, ValueError):
|
|
24
|
+
return None
|
|
25
|
+
if project.get("name") != DISTRIBUTION_NAME:
|
|
26
|
+
return None
|
|
27
|
+
version = project.get("version")
|
|
28
|
+
return version if isinstance(version, str) else None
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _resolve() -> str:
|
|
32
|
+
try:
|
|
33
|
+
return metadata.version(DISTRIBUTION_NAME)
|
|
34
|
+
except metadata.PackageNotFoundError:
|
|
35
|
+
return _from_source_checkout() or "unknown"
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
__version__ = _resolve()
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# Copyright 2026 Brafford Labs, LLC
|
|
3
|
+
"""Domain adapters for type-specific validation and side effects."""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from abc import ABC, abstractmethod
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
from governed_objects.adapters.context import AdapterContext, AdapterExecuteResult, HydratedObject
|
|
11
|
+
from governed_objects.domain.command import Command
|
|
12
|
+
from governed_objects.domain.models import Actor, CommandResult, GovernedObject, ValidationCheck
|
|
13
|
+
from governed_objects.types.schema import ObjectTypeDefinition
|
|
14
|
+
from governed_objects.observations.validation import CandidateImage, CandidateMode, CandidateVerdict
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class TypeAdapter(ABC):
|
|
18
|
+
"""Hook for domain-specific validation, hydration, and command handling."""
|
|
19
|
+
|
|
20
|
+
@abstractmethod
|
|
21
|
+
def supports(self, object_type: str) -> bool: ...
|
|
22
|
+
|
|
23
|
+
def validate_observation_candidate(
|
|
24
|
+
self, *, type_def: ObjectTypeDefinition, current: CandidateImage | None,
|
|
25
|
+
candidate: CandidateImage, mode: CandidateMode,
|
|
26
|
+
) -> CandidateVerdict:
|
|
27
|
+
"""Pure full-state/data invariant check; no create-validator fallback."""
|
|
28
|
+
return CandidateVerdict.UNAVAILABLE
|
|
29
|
+
|
|
30
|
+
@abstractmethod
|
|
31
|
+
def validate_create(
|
|
32
|
+
self,
|
|
33
|
+
type_def: ObjectTypeDefinition,
|
|
34
|
+
data: dict[str, Any],
|
|
35
|
+
) -> tuple[bool, list[ValidationCheck], str]: ...
|
|
36
|
+
|
|
37
|
+
@abstractmethod
|
|
38
|
+
def validate_transition(
|
|
39
|
+
self,
|
|
40
|
+
obj: GovernedObject,
|
|
41
|
+
type_def: ObjectTypeDefinition,
|
|
42
|
+
action: str,
|
|
43
|
+
actor: Actor,
|
|
44
|
+
) -> tuple[bool, list[ValidationCheck], str]: ...
|
|
45
|
+
|
|
46
|
+
def load(
|
|
47
|
+
self,
|
|
48
|
+
obj: GovernedObject,
|
|
49
|
+
context: AdapterContext,
|
|
50
|
+
) -> HydratedObject | None:
|
|
51
|
+
"""Hydrate canonical view from backing implementation. None = use stored object."""
|
|
52
|
+
return None
|
|
53
|
+
|
|
54
|
+
def adapter_execute(
|
|
55
|
+
self,
|
|
56
|
+
obj: GovernedObject,
|
|
57
|
+
command: Command,
|
|
58
|
+
context: AdapterContext,
|
|
59
|
+
) -> AdapterExecuteResult | None:
|
|
60
|
+
"""Execute mutation via backing implementation. None = local runtime handles it."""
|
|
61
|
+
return None
|
|
62
|
+
|
|
63
|
+
def execute(
|
|
64
|
+
self,
|
|
65
|
+
command: Command,
|
|
66
|
+
obj: GovernedObject | None,
|
|
67
|
+
type_def: ObjectTypeDefinition,
|
|
68
|
+
actor: Actor,
|
|
69
|
+
) -> CommandResult | None:
|
|
70
|
+
"""Optional override for non-standard commands (e.g. a type-specific command)."""
|
|
71
|
+
return None
|
|
72
|
+
|
|
73
|
+
def filter_read_data(
|
|
74
|
+
self,
|
|
75
|
+
obj: GovernedObject,
|
|
76
|
+
type_def: ObjectTypeDefinition,
|
|
77
|
+
actor: Actor,
|
|
78
|
+
fields: list[str],
|
|
79
|
+
) -> tuple[dict[str, Any], list[str]]:
|
|
80
|
+
return obj.data, []
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# Copyright 2026 Brafford Labs, LLC
|
|
3
|
+
"""Adapter execution context and results."""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from dataclasses import dataclass, field
|
|
8
|
+
from datetime import UTC, datetime
|
|
9
|
+
from enum import StrEnum
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
from governed_objects.domain.command_context import CommandContext
|
|
13
|
+
from governed_objects.domain.models import GovernedObject, Relationship
|
|
14
|
+
from governed_objects.persistence.sqlite import SQLiteStore
|
|
15
|
+
from governed_objects.providers.registry import ProviderRegistry
|
|
16
|
+
from governed_objects.types.registry import TypeRegistry
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@dataclass
|
|
20
|
+
class AdapterContext:
|
|
21
|
+
store: SQLiteStore
|
|
22
|
+
providers: ProviderRegistry
|
|
23
|
+
type_registry: TypeRegistry
|
|
24
|
+
command_context: CommandContext | None = None
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@dataclass
|
|
28
|
+
class HydratedObject:
|
|
29
|
+
"""Provider-observed view — not accepted canonical truth until ingested."""
|
|
30
|
+
|
|
31
|
+
object: GovernedObject
|
|
32
|
+
relationships: list[Relationship] = field(default_factory=list)
|
|
33
|
+
provider_metadata: dict[str, Any] = field(default_factory=dict)
|
|
34
|
+
provider_observation: dict[str, Any] | None = None
|
|
35
|
+
observed_at: datetime = field(default_factory=lambda: datetime.now(UTC))
|
|
36
|
+
source: str = "local"
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class AdapterFailure(StrEnum):
|
|
40
|
+
"""Why an adapter did not execute a mutation."""
|
|
41
|
+
|
|
42
|
+
INVALID = "invalid" # the requested change violates the type's declared rules
|
|
43
|
+
PROVIDER = "provider" # the backing provider failed on the request
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
@dataclass
|
|
47
|
+
class AdapterExecuteResult:
|
|
48
|
+
"""``failure`` is None when ``success`` is true and MUST classify the
|
|
49
|
+
failure when it is false. The runtime refuses a result that breaks this
|
|
50
|
+
(``ADAPTER_RESULT_INVALID``) rather than guessing the failure class."""
|
|
51
|
+
|
|
52
|
+
success: bool
|
|
53
|
+
object: GovernedObject | None = None
|
|
54
|
+
relationships: list[Relationship] = field(default_factory=list)
|
|
55
|
+
provider_metadata: dict[str, Any] = field(default_factory=dict)
|
|
56
|
+
error_code: str | None = None
|
|
57
|
+
error_message: str | None = None
|
|
58
|
+
failure: AdapterFailure | None = None
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# Copyright 2026 Brafford Labs, LLC
|
|
3
|
+
"""DelegatedMandate adapter — schema validation only."""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
from governed_objects.adapters.base import TypeAdapter
|
|
10
|
+
from governed_objects.domain.models import Actor, GovernedObject, ValidationCheck
|
|
11
|
+
from governed_objects.types.schema import ObjectTypeDefinition
|
|
12
|
+
from governed_objects.types.validation import SchemaValidator
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class MandateAdapter(TypeAdapter):
|
|
16
|
+
def supports(self, object_type: str) -> bool:
|
|
17
|
+
return object_type == "DelegatedMandate"
|
|
18
|
+
|
|
19
|
+
def validate_create(
|
|
20
|
+
self,
|
|
21
|
+
type_def: ObjectTypeDefinition,
|
|
22
|
+
data: dict[str, Any],
|
|
23
|
+
) -> tuple[bool, list[ValidationCheck], str]:
|
|
24
|
+
valid, checks, errors = SchemaValidator().validate(type_def, data)
|
|
25
|
+
if not data.get("capabilities"):
|
|
26
|
+
return False, checks, "capabilities required"
|
|
27
|
+
return valid, checks, "; ".join(errors)
|
|
28
|
+
|
|
29
|
+
def validate_transition(
|
|
30
|
+
self,
|
|
31
|
+
obj: GovernedObject,
|
|
32
|
+
type_def: ObjectTypeDefinition,
|
|
33
|
+
action: str,
|
|
34
|
+
actor: Actor,
|
|
35
|
+
) -> tuple[bool, list[ValidationCheck], str]:
|
|
36
|
+
return True, [], ""
|
|
37
|
+
|
|
38
|
+
def filter_read_data(
|
|
39
|
+
self,
|
|
40
|
+
obj: GovernedObject,
|
|
41
|
+
type_def: ObjectTypeDefinition,
|
|
42
|
+
actor: Actor,
|
|
43
|
+
fields: list[str],
|
|
44
|
+
) -> tuple[dict[str, Any], list[str]]:
|
|
45
|
+
return obj.data, []
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# Copyright 2026 Brafford Labs, LLC
|
|
3
|
+
"""Adapter registry — resolves the adapter for a governed object."""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from governed_objects.adapters.base import TypeAdapter
|
|
8
|
+
from governed_objects.domain.models import GovernedObject
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class AdapterRegistry:
|
|
12
|
+
def __init__(self, adapters: list[TypeAdapter]) -> None:
|
|
13
|
+
self._adapters = adapters
|
|
14
|
+
|
|
15
|
+
def resolve(self, obj: GovernedObject) -> TypeAdapter:
|
|
16
|
+
for adapter in self._adapters:
|
|
17
|
+
if adapter.supports(obj.type):
|
|
18
|
+
return adapter
|
|
19
|
+
raise KeyError(f"no adapter for type: {obj.type}")
|
|
20
|
+
|
|
21
|
+
def for_type(self, object_type: str) -> TypeAdapter | None:
|
|
22
|
+
for adapter in self._adapters:
|
|
23
|
+
if adapter.supports(object_type):
|
|
24
|
+
return adapter
|
|
25
|
+
return None
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# Copyright 2026 Brafford Labs, LLC
|
|
3
|
+
"""Thin agent session wrapper over the governed object runtime."""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from governed_objects.domain.command import Command, ReadObject
|
|
8
|
+
from governed_objects.domain.command_context import CommandContext
|
|
9
|
+
from governed_objects.domain.models import Actor, CommandResult, Decision, GovernanceEvent, GovernedObject
|
|
10
|
+
from governed_objects.domain.traversal import TraversalResult
|
|
11
|
+
from governed_objects.policy.context import AuthorizationContext, AuthorizationContextError
|
|
12
|
+
from governed_objects.runtime.runtime import GovernedObjectRuntime
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class GovernedAgentSession:
|
|
16
|
+
"""Carries agent, application, mandate, and purpose on every operation."""
|
|
17
|
+
|
|
18
|
+
def __init__(
|
|
19
|
+
self,
|
|
20
|
+
runtime: GovernedObjectRuntime,
|
|
21
|
+
*,
|
|
22
|
+
actor: Actor,
|
|
23
|
+
application_id: str,
|
|
24
|
+
mandate_id: str,
|
|
25
|
+
purpose: str,
|
|
26
|
+
) -> None:
|
|
27
|
+
if (not isinstance(actor, Actor)
|
|
28
|
+
or any(not isinstance(value, str) or not value for value in (application_id, mandate_id))
|
|
29
|
+
or (purpose is not None and (not isinstance(purpose, str) or not purpose))):
|
|
30
|
+
raise AuthorizationContextError()
|
|
31
|
+
self._runtime = runtime
|
|
32
|
+
self._actor = actor
|
|
33
|
+
self._application_id = application_id
|
|
34
|
+
self._mandate_id = mandate_id
|
|
35
|
+
self._purpose = purpose
|
|
36
|
+
|
|
37
|
+
@property
|
|
38
|
+
def actor(self) -> Actor:
|
|
39
|
+
return self._actor
|
|
40
|
+
|
|
41
|
+
@property
|
|
42
|
+
def auth(self) -> AuthorizationContext:
|
|
43
|
+
return self._runtime.resolve_authorization_context(AuthorizationContext(
|
|
44
|
+
application_id=self._application_id,
|
|
45
|
+
mandate_id=self._mandate_id,
|
|
46
|
+
purpose=self._purpose,
|
|
47
|
+
))
|
|
48
|
+
|
|
49
|
+
def _ctx(self, *, expected_version: int | None = None) -> CommandContext:
|
|
50
|
+
return CommandContext(
|
|
51
|
+
application_id=self._application_id,
|
|
52
|
+
mandate_id=self._mandate_id,
|
|
53
|
+
purpose=self._purpose,
|
|
54
|
+
expected_version=expected_version,
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
def query(
|
|
58
|
+
self,
|
|
59
|
+
*,
|
|
60
|
+
object_type: str | None = None,
|
|
61
|
+
state: str | None = None,
|
|
62
|
+
include_subtypes: bool = False,
|
|
63
|
+
) -> list[GovernedObject]:
|
|
64
|
+
return self._runtime.query(
|
|
65
|
+
self._actor,
|
|
66
|
+
object_type=object_type,
|
|
67
|
+
state=state,
|
|
68
|
+
include_subtypes=include_subtypes,
|
|
69
|
+
auth=self.auth,
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
def read(self, object_id: str) -> CommandResult:
|
|
73
|
+
return self._runtime.execute(
|
|
74
|
+
ReadObject(object_id),
|
|
75
|
+
actor=self._actor,
|
|
76
|
+
command_context=self._ctx(),
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
def traverse(
|
|
80
|
+
self,
|
|
81
|
+
start_object_id: str,
|
|
82
|
+
*,
|
|
83
|
+
max_depth: int = 3,
|
|
84
|
+
relations: list[str] | None = None,
|
|
85
|
+
) -> list[TraversalResult]:
|
|
86
|
+
return self._runtime.traverse(
|
|
87
|
+
self._actor,
|
|
88
|
+
start_object_id,
|
|
89
|
+
max_depth=max_depth,
|
|
90
|
+
relations=relations,
|
|
91
|
+
auth=self.auth,
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
def execute(
|
|
95
|
+
self,
|
|
96
|
+
command: Command,
|
|
97
|
+
*,
|
|
98
|
+
expected_version: int | None = None,
|
|
99
|
+
) -> CommandResult:
|
|
100
|
+
return self._runtime.execute(
|
|
101
|
+
command,
|
|
102
|
+
actor=self._actor,
|
|
103
|
+
command_context=self._ctx(expected_version=expected_version),
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
def history(self, object_id: str) -> list[GovernanceEvent]:
|
|
107
|
+
"""Return event history only if read is authorized."""
|
|
108
|
+
read = self.read(object_id)
|
|
109
|
+
if read.decision != Decision.ALLOW:
|
|
110
|
+
return []
|
|
111
|
+
return self._runtime.history(object_id)
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# Copyright 2026 Brafford Labs, LLC
|
|
3
|
+
"""Application identity registry with capability boundaries."""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from dataclasses import dataclass, field
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@dataclass(frozen=True)
|
|
11
|
+
class ApplicationDefinition:
|
|
12
|
+
id: str
|
|
13
|
+
name: str
|
|
14
|
+
capabilities: frozenset[str] = field(default_factory=frozenset)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class ApplicationRegistry:
|
|
18
|
+
"""Small registry of application identities for provenance and authority bounds."""
|
|
19
|
+
|
|
20
|
+
def __init__(self) -> None:
|
|
21
|
+
self._apps: dict[str, ApplicationDefinition] = {}
|
|
22
|
+
|
|
23
|
+
def register(self, app: ApplicationDefinition) -> None:
|
|
24
|
+
self._apps[app.id] = app
|
|
25
|
+
|
|
26
|
+
def get(self, app_id: str) -> ApplicationDefinition | None:
|
|
27
|
+
return self._apps.get(app_id)
|
|
28
|
+
|
|
29
|
+
def require(self, app_id: str) -> ApplicationDefinition:
|
|
30
|
+
app = self.get(app_id)
|
|
31
|
+
if app is None:
|
|
32
|
+
raise KeyError(f"unknown application: {app_id}")
|
|
33
|
+
return app
|
|
34
|
+
|
|
35
|
+
def unregister(self, app_id: str) -> None:
|
|
36
|
+
self._apps.pop(app_id, None)
|
|
37
|
+
|
|
38
|
+
def list_ids(self) -> list[str]:
|
|
39
|
+
return sorted(self._apps)
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# Copyright 2026 Brafford Labs, LLC
|
|
3
|
+
"""Type-driven authority rules for external observations."""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from enum import StrEnum
|
|
8
|
+
|
|
9
|
+
from pydantic import BaseModel, Field
|
|
10
|
+
|
|
11
|
+
from governed_objects.types.registry import TypeRegistry
|
|
12
|
+
from governed_objects.types.schema import ObjectTypeDefinition
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class AuthorityMode(StrEnum):
|
|
16
|
+
PROVIDER_AUTO_ACCEPT = "PROVIDER_AUTO_ACCEPT"
|
|
17
|
+
CANONICAL_AUTHORITATIVE = "CANONICAL_AUTHORITATIVE"
|
|
18
|
+
REQUIRES_REVIEW = "REQUIRES_REVIEW"
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class AuthorityFieldRule(BaseModel):
|
|
22
|
+
mode: AuthorityMode
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class AuthorityRules(BaseModel):
|
|
26
|
+
fields: dict[str, AuthorityFieldRule] = Field(default_factory=dict)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class AuthorityRuleResolver:
|
|
30
|
+
"""Resolve field authority with type inheritance."""
|
|
31
|
+
|
|
32
|
+
def __init__(self, type_registry: TypeRegistry) -> None:
|
|
33
|
+
self._types = type_registry
|
|
34
|
+
|
|
35
|
+
def rules_for(self, object_type: str) -> AuthorityRules:
|
|
36
|
+
return self.rules_for_definitions(tuple(self._inheritance_chain(object_type)))
|
|
37
|
+
|
|
38
|
+
@staticmethod
|
|
39
|
+
def rules_for_definitions(definitions: tuple[ObjectTypeDefinition, ...]) -> AuthorityRules:
|
|
40
|
+
"""Merge already captured parent-first definitions, with no registry I/O."""
|
|
41
|
+
merged: dict[str, AuthorityFieldRule] = {}
|
|
42
|
+
for type_def in definitions:
|
|
43
|
+
raw = type_def.extra.get("authority") or {}
|
|
44
|
+
fields = raw.get("fields") or {}
|
|
45
|
+
for field_name, spec in fields.items():
|
|
46
|
+
if isinstance(spec, dict):
|
|
47
|
+
merged[field_name] = AuthorityFieldRule(mode=AuthorityMode(spec["mode"]))
|
|
48
|
+
else:
|
|
49
|
+
merged[field_name] = AuthorityFieldRule(mode=AuthorityMode(str(spec)))
|
|
50
|
+
return AuthorityRules(fields=merged)
|
|
51
|
+
|
|
52
|
+
def mode_for_field(self, object_type: str, field_name: str) -> AuthorityMode | None:
|
|
53
|
+
return self.rules_for(object_type).fields.get(field_name, AuthorityFieldRule(mode=AuthorityMode.REQUIRES_REVIEW)).mode
|
|
54
|
+
|
|
55
|
+
def _inheritance_chain(self, object_type: str) -> list[ObjectTypeDefinition]:
|
|
56
|
+
chain: list[ObjectTypeDefinition] = []
|
|
57
|
+
current = self._types.get(object_type)
|
|
58
|
+
while current.kind not in {t.kind for t in chain}:
|
|
59
|
+
chain.append(current)
|
|
60
|
+
if not current.extends:
|
|
61
|
+
break
|
|
62
|
+
current = self._types.get(current.extends)
|
|
63
|
+
return list(reversed(chain))
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# Copyright 2026 Brafford Labs, LLC
|
|
3
|
+
"""Explicit local binding for the supported optional mandate interpretation.
|
|
4
|
+
|
|
5
|
+
No resource discovery or installation. The definition fingerprint denotes the
|
|
6
|
+
existing effective version-1 representation, not a normative profile standard.
|
|
7
|
+
Actor resolution is supplied local integration trust, not authentication.
|
|
8
|
+
"""
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from collections.abc import Callable
|
|
12
|
+
from dataclasses import dataclass
|
|
13
|
+
import hashlib
|
|
14
|
+
import json
|
|
15
|
+
from typing import TYPE_CHECKING
|
|
16
|
+
|
|
17
|
+
from governed_objects.domain.models import Actor, GovernedObject
|
|
18
|
+
from governed_objects.types.schema import ObjectTypeDefinition
|
|
19
|
+
|
|
20
|
+
if TYPE_CHECKING:
|
|
21
|
+
from governed_objects.adapters.mandate import MandateAdapter
|
|
22
|
+
from governed_objects.events.reducers.mandate import MandateEventReducer
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class DelegationConfigurationError(ValueError):
|
|
26
|
+
def __init__(self):
|
|
27
|
+
super().__init__('delegation configuration unavailable')
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _identity(definition):
|
|
31
|
+
return hashlib.sha256(json.dumps(definition.model_dump(mode='json'), sort_keys=True,
|
|
32
|
+
separators=(',', ':')).encode()).hexdigest()
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@dataclass(frozen=True)
|
|
36
|
+
class DelegationConfiguration:
|
|
37
|
+
definition: ObjectTypeDefinition
|
|
38
|
+
adapter: MandateAdapter
|
|
39
|
+
reducer: MandateEventReducer
|
|
40
|
+
scope_decoder: Callable
|
|
41
|
+
actor_source: Callable[[str], Actor]
|
|
42
|
+
|
|
43
|
+
def __post_init__(self):
|
|
44
|
+
from governed_objects.adapters.mandate import MandateAdapter
|
|
45
|
+
from governed_objects.events.reducers.mandate import MandateEventReducer
|
|
46
|
+
|
|
47
|
+
if (not isinstance(self.definition, ObjectTypeDefinition)
|
|
48
|
+
or _identity(self.definition) != '7ad9425265abc99d9488be22483037c37fcd1815a7a74c6544c8d9c19d8800cb'
|
|
49
|
+
or type(self.adapter) is not MandateAdapter
|
|
50
|
+
or type(self.reducer) is not MandateEventReducer
|
|
51
|
+
or not callable(self.scope_decoder) or not callable(self.actor_source)):
|
|
52
|
+
raise DelegationConfigurationError()
|
|
53
|
+
|
|
54
|
+
def validate(self, types, adapters, extensions, obj: GovernedObject | None = None):
|
|
55
|
+
self.__post_init__()
|
|
56
|
+
try:
|
|
57
|
+
current = types.get(self.definition.kind)
|
|
58
|
+
except KeyError:
|
|
59
|
+
raise DelegationConfigurationError() from None
|
|
60
|
+
if (current is not self.definition or _identity(current) != _identity(self.definition)
|
|
61
|
+
or adapters.for_type(self.definition.kind) is not self.adapter
|
|
62
|
+
or extensions.reducer_for(self.definition.kind) is not self.reducer
|
|
63
|
+
or (obj is not None and (obj.type != self.definition.kind
|
|
64
|
+
or obj.type_version != self.definition.type_version))):
|
|
65
|
+
raise DelegationConfigurationError()
|
|
66
|
+
|
|
67
|
+
def actor(self, identity: str) -> Actor:
|
|
68
|
+
try:
|
|
69
|
+
actor = self.actor_source(identity)
|
|
70
|
+
except Exception:
|
|
71
|
+
raise DelegationConfigurationError() from None
|
|
72
|
+
if not isinstance(actor, Actor) or actor.id != identity:
|
|
73
|
+
raise DelegationConfigurationError()
|
|
74
|
+
return actor
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# Copyright 2026 Brafford Labs, LLC
|
|
3
|
+
"""Delegated mandate model."""
|
|
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.scope import MandateScope
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class MandateStatus(StrEnum):
|
|
15
|
+
DRAFT = "draft"
|
|
16
|
+
ACTIVE = "active"
|
|
17
|
+
REVOKED = "revoked"
|
|
18
|
+
EXPIRED = "expired"
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@dataclass
|
|
22
|
+
class DelegatedMandate:
|
|
23
|
+
id: str
|
|
24
|
+
principal_id: str
|
|
25
|
+
delegate_actor_id: str
|
|
26
|
+
capabilities: frozenset[str]
|
|
27
|
+
scope: MandateScope = field(default_factory=MandateScope)
|
|
28
|
+
purposes: frozenset[str] = frozenset()
|
|
29
|
+
valid_from: datetime | None = None
|
|
30
|
+
valid_until: datetime | None = None
|
|
31
|
+
status: MandateStatus = MandateStatus.DRAFT
|
|
32
|
+
parent_mandate_id: str | None = None
|
|
33
|
+
version: int = 1
|
|
34
|
+
|
|
35
|
+
def is_active_at(self, when: datetime) -> bool:
|
|
36
|
+
if self.status == MandateStatus.REVOKED:
|
|
37
|
+
return False
|
|
38
|
+
if self.status == MandateStatus.DRAFT:
|
|
39
|
+
return False
|
|
40
|
+
if self.valid_from is not None and when < self.valid_from:
|
|
41
|
+
return False
|
|
42
|
+
if self.valid_until is not None and when > self.valid_until:
|
|
43
|
+
return False
|
|
44
|
+
return self.status == MandateStatus.ACTIVE
|