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,67 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# Copyright 2026 Brafford Labs, LLC
|
|
3
|
+
"""Strict typed schemas for model-controlled tool arguments."""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
from pydantic import BaseModel, ConfigDict, ValidationError
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class QueryObjectsInput(BaseModel):
|
|
13
|
+
model_config = ConfigDict(extra="forbid")
|
|
14
|
+
|
|
15
|
+
object_type: str
|
|
16
|
+
include_subtypes: bool = False
|
|
17
|
+
state: str | None = None
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class ReadObjectInput(BaseModel):
|
|
21
|
+
model_config = ConfigDict(extra="forbid")
|
|
22
|
+
|
|
23
|
+
object_id: str
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class TraverseRelationshipsInput(BaseModel):
|
|
27
|
+
model_config = ConfigDict(extra="forbid")
|
|
28
|
+
|
|
29
|
+
object_id: str
|
|
30
|
+
relation: str | None = None
|
|
31
|
+
max_depth: int = 2
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class ExecuteActionInput(BaseModel):
|
|
35
|
+
model_config = ConfigDict(extra="forbid")
|
|
36
|
+
|
|
37
|
+
object_id: str
|
|
38
|
+
action: str
|
|
39
|
+
expected_version: int
|
|
40
|
+
parameters: dict[str, Any] = {}
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class GetHistoryInput(BaseModel):
|
|
44
|
+
model_config = ConfigDict(extra="forbid")
|
|
45
|
+
|
|
46
|
+
object_id: str
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
FORBIDDEN_IDENTITY_FIELDS = frozenset({
|
|
50
|
+
"actor_id",
|
|
51
|
+
"application_id",
|
|
52
|
+
"mandate_id",
|
|
53
|
+
"principal_id",
|
|
54
|
+
"purpose",
|
|
55
|
+
"provider",
|
|
56
|
+
"runtime",
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def parse_tool_input(schema: type[BaseModel], arguments: dict[str, Any]) -> tuple[BaseModel | None, str | None]:
|
|
61
|
+
for key in arguments:
|
|
62
|
+
if key in FORBIDDEN_IDENTITY_FIELDS:
|
|
63
|
+
return None, f"forbidden field: {key}"
|
|
64
|
+
try:
|
|
65
|
+
return schema.model_validate(arguments), None
|
|
66
|
+
except ValidationError as exc:
|
|
67
|
+
return None, str(exc.errors()[0]["msg"])
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# Copyright 2026 Brafford Labs, LLC
|
|
3
|
+
"""Experimental optional-use tool surface over a trusted bound agent session."""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
from governed_objects.agent.session import GovernedAgentSession
|
|
10
|
+
from governed_objects.domain.command import TransitionState
|
|
11
|
+
from governed_objects.domain.models import Decision
|
|
12
|
+
from governed_objects.policy.context import AuthorizationContextError
|
|
13
|
+
from governed_objects.llm.model import ToolCall, ToolDefinition
|
|
14
|
+
from governed_objects.llm.tools.registry import is_registered_tool, tool_definitions
|
|
15
|
+
from governed_objects.llm.tools.results import ToolResult
|
|
16
|
+
from governed_objects.llm.tools.schemas import (
|
|
17
|
+
ExecuteActionInput,
|
|
18
|
+
GetHistoryInput,
|
|
19
|
+
QueryObjectsInput,
|
|
20
|
+
ReadObjectInput,
|
|
21
|
+
TraverseRelationshipsInput,
|
|
22
|
+
parse_tool_input,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class GovernedAgentTools:
|
|
27
|
+
"""Fixed tool surface — model receives this, not the runtime."""
|
|
28
|
+
|
|
29
|
+
def __init__(self, session: GovernedAgentSession) -> None:
|
|
30
|
+
self._session = session
|
|
31
|
+
|
|
32
|
+
@property
|
|
33
|
+
def session(self) -> GovernedAgentSession:
|
|
34
|
+
return self._session
|
|
35
|
+
|
|
36
|
+
@property
|
|
37
|
+
def bound_context(self) -> dict[str, str]:
|
|
38
|
+
mandate = self._session.auth.mandate
|
|
39
|
+
return {
|
|
40
|
+
"actor_id": self._session.actor.id,
|
|
41
|
+
"application_id": self._session._application_id,
|
|
42
|
+
"mandate_id": self._session._mandate_id,
|
|
43
|
+
"principal_id": mandate.principal_id if mandate else "",
|
|
44
|
+
"purpose": self._session._purpose,
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
def definitions(self) -> list[ToolDefinition]:
|
|
48
|
+
return tool_definitions()
|
|
49
|
+
|
|
50
|
+
def execute(self, tool_call: ToolCall) -> ToolResult:
|
|
51
|
+
if not is_registered_tool(tool_call.name):
|
|
52
|
+
return ToolResult(
|
|
53
|
+
tool_call_id=tool_call.id,
|
|
54
|
+
tool_name=tool_call.name,
|
|
55
|
+
success=False,
|
|
56
|
+
error=f"unknown tool: {tool_call.name}",
|
|
57
|
+
)
|
|
58
|
+
handler = {
|
|
59
|
+
"query_objects": self._query_objects,
|
|
60
|
+
"read_object": self._read_object,
|
|
61
|
+
"traverse_relationships": self._traverse_relationships,
|
|
62
|
+
"execute_action": self._execute_action,
|
|
63
|
+
"get_history": self._get_history,
|
|
64
|
+
}[tool_call.name]
|
|
65
|
+
try:
|
|
66
|
+
return handler(tool_call)
|
|
67
|
+
except AuthorizationContextError:
|
|
68
|
+
return ToolResult(
|
|
69
|
+
tool_call_id=tool_call.id,
|
|
70
|
+
tool_name=tool_call.name,
|
|
71
|
+
success=False,
|
|
72
|
+
error="authorization context unavailable",
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
def _query_objects(self, tool_call: ToolCall) -> ToolResult:
|
|
76
|
+
parsed, err = parse_tool_input(QueryObjectsInput, tool_call.arguments)
|
|
77
|
+
if err:
|
|
78
|
+
return self._schema_error(tool_call, err)
|
|
79
|
+
assert parsed is not None
|
|
80
|
+
objects = self._session.query(
|
|
81
|
+
object_type=parsed.object_type,
|
|
82
|
+
state=parsed.state,
|
|
83
|
+
include_subtypes=parsed.include_subtypes,
|
|
84
|
+
)
|
|
85
|
+
return ToolResult(
|
|
86
|
+
tool_call_id=tool_call.id,
|
|
87
|
+
tool_name=tool_call.name,
|
|
88
|
+
success=True,
|
|
89
|
+
decision="ALLOW",
|
|
90
|
+
objects=[
|
|
91
|
+
{
|
|
92
|
+
"id": o.id,
|
|
93
|
+
"type": o.type,
|
|
94
|
+
"state": o.state,
|
|
95
|
+
"version": o.version,
|
|
96
|
+
"data": o.data,
|
|
97
|
+
}
|
|
98
|
+
for o in objects
|
|
99
|
+
],
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
def _read_object(self, tool_call: ToolCall) -> ToolResult:
|
|
103
|
+
parsed, err = parse_tool_input(ReadObjectInput, tool_call.arguments)
|
|
104
|
+
if err:
|
|
105
|
+
return self._schema_error(tool_call, err)
|
|
106
|
+
assert parsed is not None
|
|
107
|
+
result = self._session.read(parsed.object_id)
|
|
108
|
+
if result.decision == Decision.DENY:
|
|
109
|
+
return ToolResult(
|
|
110
|
+
tool_call_id=tool_call.id,
|
|
111
|
+
tool_name=tool_call.name,
|
|
112
|
+
success=True,
|
|
113
|
+
decision="DENY",
|
|
114
|
+
reason=result.deny_reason or "ACCESS_DENIED",
|
|
115
|
+
object_id=parsed.object_id,
|
|
116
|
+
message="Object unavailable or access denied.",
|
|
117
|
+
)
|
|
118
|
+
stored = self._session._runtime.get_object(parsed.object_id)
|
|
119
|
+
state = stored.state if stored else result.new_state
|
|
120
|
+
version = stored.version if stored else result.object_version
|
|
121
|
+
return ToolResult(
|
|
122
|
+
tool_call_id=tool_call.id,
|
|
123
|
+
tool_name=tool_call.name,
|
|
124
|
+
success=True,
|
|
125
|
+
decision="ALLOW",
|
|
126
|
+
object_id=parsed.object_id,
|
|
127
|
+
state=state,
|
|
128
|
+
version=version,
|
|
129
|
+
objects=[{
|
|
130
|
+
"id": parsed.object_id,
|
|
131
|
+
"state": state,
|
|
132
|
+
"version": version,
|
|
133
|
+
"data": result.data,
|
|
134
|
+
}],
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
def _traverse_relationships(self, tool_call: ToolCall) -> ToolResult:
|
|
138
|
+
parsed, err = parse_tool_input(TraverseRelationshipsInput, tool_call.arguments)
|
|
139
|
+
if err:
|
|
140
|
+
return self._schema_error(tool_call, err)
|
|
141
|
+
assert parsed is not None
|
|
142
|
+
relations = [parsed.relation] if parsed.relation else None
|
|
143
|
+
results = self._session.traverse(
|
|
144
|
+
parsed.object_id,
|
|
145
|
+
max_depth=parsed.max_depth,
|
|
146
|
+
relations=relations,
|
|
147
|
+
)
|
|
148
|
+
return ToolResult(
|
|
149
|
+
tool_call_id=tool_call.id,
|
|
150
|
+
tool_name=tool_call.name,
|
|
151
|
+
success=True,
|
|
152
|
+
decision="ALLOW",
|
|
153
|
+
objects=[
|
|
154
|
+
{
|
|
155
|
+
"id": r.object.id,
|
|
156
|
+
"type": r.object.type,
|
|
157
|
+
"state": r.object.state,
|
|
158
|
+
"version": r.object.version,
|
|
159
|
+
"relation": r.path[-1].relation if r.path else None,
|
|
160
|
+
"data": r.object.data,
|
|
161
|
+
}
|
|
162
|
+
for r in results
|
|
163
|
+
],
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
def _execute_action(self, tool_call: ToolCall) -> ToolResult:
|
|
167
|
+
parsed, err = parse_tool_input(ExecuteActionInput, tool_call.arguments)
|
|
168
|
+
if err:
|
|
169
|
+
return self._schema_error(tool_call, err)
|
|
170
|
+
assert parsed is not None
|
|
171
|
+
result = self._session.execute(
|
|
172
|
+
TransitionState(parsed.object_id, parsed.action),
|
|
173
|
+
expected_version=parsed.expected_version,
|
|
174
|
+
)
|
|
175
|
+
if result.decision == Decision.CONFLICT:
|
|
176
|
+
return ToolResult(
|
|
177
|
+
tool_call_id=tool_call.id,
|
|
178
|
+
tool_name=tool_call.name,
|
|
179
|
+
success=True,
|
|
180
|
+
decision="CONFLICT",
|
|
181
|
+
reason=result.conflict_reason,
|
|
182
|
+
object_id=parsed.object_id,
|
|
183
|
+
expected_version=result.expected_version,
|
|
184
|
+
actual_version=result.actual_version,
|
|
185
|
+
state=result.new_state,
|
|
186
|
+
message="Object changed. Refresh before deciding again.",
|
|
187
|
+
)
|
|
188
|
+
if result.decision == Decision.DENY:
|
|
189
|
+
return ToolResult(
|
|
190
|
+
tool_call_id=tool_call.id,
|
|
191
|
+
tool_name=tool_call.name,
|
|
192
|
+
success=True,
|
|
193
|
+
decision="DENY",
|
|
194
|
+
reason=result.deny_reason or "DENIED",
|
|
195
|
+
object_id=parsed.object_id,
|
|
196
|
+
state=result.new_state,
|
|
197
|
+
version=result.object_version,
|
|
198
|
+
)
|
|
199
|
+
return ToolResult(
|
|
200
|
+
tool_call_id=tool_call.id,
|
|
201
|
+
tool_name=tool_call.name,
|
|
202
|
+
success=True,
|
|
203
|
+
decision="ALLOW",
|
|
204
|
+
object_id=parsed.object_id,
|
|
205
|
+
state=result.new_state,
|
|
206
|
+
version=result.object_version,
|
|
207
|
+
)
|
|
208
|
+
|
|
209
|
+
def _get_history(self, tool_call: ToolCall) -> ToolResult:
|
|
210
|
+
parsed, err = parse_tool_input(GetHistoryInput, tool_call.arguments)
|
|
211
|
+
if err:
|
|
212
|
+
return self._schema_error(tool_call, err)
|
|
213
|
+
assert parsed is not None
|
|
214
|
+
events = self._session.history(parsed.object_id)
|
|
215
|
+
if not events:
|
|
216
|
+
read = self._session.read(parsed.object_id)
|
|
217
|
+
if read.decision != Decision.ALLOW:
|
|
218
|
+
return ToolResult(
|
|
219
|
+
tool_call_id=tool_call.id,
|
|
220
|
+
tool_name=tool_call.name,
|
|
221
|
+
success=True,
|
|
222
|
+
decision="DENY",
|
|
223
|
+
reason=read.deny_reason or "ACCESS_DENIED",
|
|
224
|
+
object_id=parsed.object_id,
|
|
225
|
+
)
|
|
226
|
+
return ToolResult(
|
|
227
|
+
tool_call_id=tool_call.id,
|
|
228
|
+
tool_name=tool_call.name,
|
|
229
|
+
success=True,
|
|
230
|
+
decision="ALLOW",
|
|
231
|
+
object_id=parsed.object_id,
|
|
232
|
+
objects=[
|
|
233
|
+
{
|
|
234
|
+
"action": e.action,
|
|
235
|
+
"decision": e.decision.value,
|
|
236
|
+
"previous_state": e.previous_state,
|
|
237
|
+
"new_state": e.new_state,
|
|
238
|
+
"object_version": e.object_version,
|
|
239
|
+
}
|
|
240
|
+
for e in events
|
|
241
|
+
],
|
|
242
|
+
)
|
|
243
|
+
|
|
244
|
+
@staticmethod
|
|
245
|
+
def _schema_error(tool_call: ToolCall, message: str) -> ToolResult:
|
|
246
|
+
return ToolResult(
|
|
247
|
+
tool_call_id=tool_call.id,
|
|
248
|
+
tool_name=tool_call.name,
|
|
249
|
+
success=False,
|
|
250
|
+
error=message,
|
|
251
|
+
schema_rejected=True,
|
|
252
|
+
)
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# Copyright 2026 Brafford Labs, LLC
|
|
3
|
+
"""Operational execution trace — separate from canonical business events."""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from dataclasses import dataclass, field
|
|
8
|
+
from typing import Any
|
|
9
|
+
from uuid import uuid4
|
|
10
|
+
|
|
11
|
+
from governed_objects.llm.tools.results import ToolResult
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@dataclass
|
|
15
|
+
class ToolTraceEntry:
|
|
16
|
+
step: int
|
|
17
|
+
tool_name: str
|
|
18
|
+
arguments: dict[str, Any]
|
|
19
|
+
result: dict[str, Any]
|
|
20
|
+
decision: str | None = None
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@dataclass
|
|
24
|
+
class AgentExecutionTrace:
|
|
25
|
+
run_id: str = field(default_factory=lambda: str(uuid4()))
|
|
26
|
+
task: str = ""
|
|
27
|
+
model_provider: str = ""
|
|
28
|
+
model_name: str = ""
|
|
29
|
+
bound_context: dict[str, str] = field(default_factory=dict)
|
|
30
|
+
tool_calls: list[ToolTraceEntry] = field(default_factory=list)
|
|
31
|
+
final_message: str | None = None
|
|
32
|
+
steps: int = 0
|
|
33
|
+
conflicts: int = 0
|
|
34
|
+
refreshes: int = 0
|
|
35
|
+
blind_retries: int = 0
|
|
36
|
+
unauthorized_attempts: int = 0
|
|
37
|
+
schema_rejections: int = 0
|
|
38
|
+
|
|
39
|
+
def record(self, step: int, tool_name: str, arguments: dict, result: ToolResult) -> None:
|
|
40
|
+
decision = result.decision or ("ERROR" if result.error else None)
|
|
41
|
+
entry = ToolTraceEntry(
|
|
42
|
+
step=step,
|
|
43
|
+
tool_name=tool_name,
|
|
44
|
+
arguments=arguments,
|
|
45
|
+
result=result.to_model_dict(),
|
|
46
|
+
decision=decision,
|
|
47
|
+
)
|
|
48
|
+
self.tool_calls.append(entry)
|
|
49
|
+
if result.decision == "CONFLICT":
|
|
50
|
+
self.conflicts += 1
|
|
51
|
+
if result.schema_rejected:
|
|
52
|
+
self.schema_rejections += 1
|
|
53
|
+
if result.decision == "DENY" and result.reason not in {
|
|
54
|
+
None,
|
|
55
|
+
"NOT_FOUND",
|
|
56
|
+
"ACCESS_DENIED",
|
|
57
|
+
}:
|
|
58
|
+
self.unauthorized_attempts += 1
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# Copyright 2026 Brafford Labs, LLC
|
|
3
|
+
"""Unified canonical object materialization — no silent provider mutation."""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from governed_objects.adapters.context import AdapterContext, HydratedObject
|
|
8
|
+
from governed_objects.adapters.registry import AdapterRegistry
|
|
9
|
+
from governed_objects.domain.models import Actor, GovernedObject
|
|
10
|
+
from governed_objects.persistence.sqlite import SQLiteStore
|
|
11
|
+
from governed_objects.relationships.sync import RelationshipSyncService
|
|
12
|
+
from governed_objects.types.registry import TypeRegistry
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class MaterializationService:
|
|
16
|
+
"""Produce canonical view; provider freshness is observed, not silently accepted."""
|
|
17
|
+
|
|
18
|
+
def __init__(
|
|
19
|
+
self,
|
|
20
|
+
store: SQLiteStore,
|
|
21
|
+
adapter_registry: AdapterRegistry,
|
|
22
|
+
type_registry: TypeRegistry,
|
|
23
|
+
rel_sync: RelationshipSyncService | None = None,
|
|
24
|
+
) -> None:
|
|
25
|
+
self._store = store
|
|
26
|
+
self._adapters = adapter_registry
|
|
27
|
+
self._types = type_registry
|
|
28
|
+
self._rel_sync = rel_sync or RelationshipSyncService(store)
|
|
29
|
+
|
|
30
|
+
def materialize_stored(
|
|
31
|
+
self,
|
|
32
|
+
stored: GovernedObject,
|
|
33
|
+
context: AdapterContext,
|
|
34
|
+
*,
|
|
35
|
+
hydrate: bool = True,
|
|
36
|
+
persist: bool = True,
|
|
37
|
+
) -> GovernedObject:
|
|
38
|
+
if not hydrate:
|
|
39
|
+
return stored
|
|
40
|
+
|
|
41
|
+
adapter = self._adapters.resolve(stored)
|
|
42
|
+
hydrated = adapter.load(stored, context)
|
|
43
|
+
if hydrated is None:
|
|
44
|
+
return stored
|
|
45
|
+
|
|
46
|
+
# Hydration observes provider view — never silently mutates canonical data/state.
|
|
47
|
+
meta = dict(stored.metadata)
|
|
48
|
+
meta["_provider_observed"] = {
|
|
49
|
+
"data": hydrated.object.data,
|
|
50
|
+
"state": hydrated.object.state,
|
|
51
|
+
"source": hydrated.source,
|
|
52
|
+
"observed_at": hydrated.observed_at.isoformat(),
|
|
53
|
+
"provider_metadata": hydrated.provider_metadata,
|
|
54
|
+
}
|
|
55
|
+
if hydrated.provider_observation is not None:
|
|
56
|
+
meta["_provider_observed"]["observation"] = hydrated.provider_observation
|
|
57
|
+
|
|
58
|
+
result = stored.model_copy(update={"metadata": meta})
|
|
59
|
+
if persist:
|
|
60
|
+
self._store.save(result)
|
|
61
|
+
return result
|
|
62
|
+
|
|
63
|
+
def observe_provider(
|
|
64
|
+
self,
|
|
65
|
+
stored: GovernedObject,
|
|
66
|
+
context: AdapterContext,
|
|
67
|
+
) -> HydratedObject | None:
|
|
68
|
+
"""Fetch provider view without persisting canonical business state."""
|
|
69
|
+
adapter = self._adapters.resolve(stored)
|
|
70
|
+
return adapter.load(stored, context)
|
|
71
|
+
|
|
72
|
+
def materialize(
|
|
73
|
+
self,
|
|
74
|
+
object_id: str,
|
|
75
|
+
context: AdapterContext,
|
|
76
|
+
actor: Actor | None = None,
|
|
77
|
+
*,
|
|
78
|
+
hydrate: bool = True,
|
|
79
|
+
field_filter: bool = False,
|
|
80
|
+
fields: list[str] | None = None,
|
|
81
|
+
persist: bool = True,
|
|
82
|
+
) -> GovernedObject | None:
|
|
83
|
+
stored = self._store.get(object_id)
|
|
84
|
+
if stored is None:
|
|
85
|
+
return None
|
|
86
|
+
|
|
87
|
+
obj = self.materialize_stored(stored, context, hydrate=hydrate, persist=persist)
|
|
88
|
+
|
|
89
|
+
if actor and field_filter:
|
|
90
|
+
obj = self._apply_field_filter(obj, actor, fields or [])
|
|
91
|
+
return obj
|
|
92
|
+
|
|
93
|
+
def _apply_field_filter(
|
|
94
|
+
self,
|
|
95
|
+
obj: GovernedObject,
|
|
96
|
+
actor: Actor,
|
|
97
|
+
fields: list[str],
|
|
98
|
+
) -> GovernedObject:
|
|
99
|
+
adapter = self._adapters.resolve(obj)
|
|
100
|
+
try:
|
|
101
|
+
type_def = self._types.get(obj.type)
|
|
102
|
+
except KeyError:
|
|
103
|
+
return obj
|
|
104
|
+
filtered, _ = adapter.filter_read_data(obj, type_def, actor, fields)
|
|
105
|
+
if filtered == obj.data:
|
|
106
|
+
return obj
|
|
107
|
+
return obj.model_copy(update={"data": filtered})
|