itself 0.2.0__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.
- itself/__init__.py +282 -0
- itself/_endpoints.py +255 -0
- itself/_filesystem.py +187 -0
- itself/_formats.py +104 -0
- itself/_json.py +223 -0
- itself/_version.py +8 -0
- itself/bundle.py +723 -0
- itself/cli.py +719 -0
- itself/decision_model.py +1283 -0
- itself/evidence_bundle.py +981 -0
- itself/inference.py +1571 -0
- itself/ledger.py +373 -0
- itself/py.typed +0 -0
- itself/receipts.py +621 -0
- itself/records.py +743 -0
- itself/reporting.py +125 -0
- itself/schema_export.py +178 -0
- itself/schemas/v0alpha/evidence-bundle.schema.json +144 -0
- itself/schemas/v0alpha/protocol.schema.json +466 -0
- itself/schemas/v0alpha/reasoning-receipt.schema.json +366 -0
- itself/schemas/v0alpha2/evidence-bundle.schema.json +145 -0
- itself/schemas/v0alpha2/protocol.schema.json +503 -0
- itself/schemas/v0alpha2/reasoning-receipt.schema.json +406 -0
- itself/state.py +178 -0
- itself/types.py +21 -0
- itself/validation.py +185 -0
- itself-0.2.0.dist-info/METADATA +508 -0
- itself-0.2.0.dist-info/RECORD +32 -0
- itself-0.2.0.dist-info/WHEEL +5 -0
- itself-0.2.0.dist-info/entry_points.txt +2 -0
- itself-0.2.0.dist-info/licenses/LICENSE +373 -0
- itself-0.2.0.dist-info/top_level.txt +1 -0
itself/records.py
ADDED
|
@@ -0,0 +1,743 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
# Copyright (C) 2026 Greater Expanse LLC
|
|
3
|
+
|
|
4
|
+
"""Typed construction helpers for Claim and Evidence Protocol records."""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import json
|
|
9
|
+
from collections.abc import Mapping, Sequence
|
|
10
|
+
from copy import deepcopy
|
|
11
|
+
from dataclasses import dataclass, field
|
|
12
|
+
from datetime import UTC, datetime
|
|
13
|
+
from enum import StrEnum
|
|
14
|
+
from functools import cache
|
|
15
|
+
from types import MappingProxyType
|
|
16
|
+
from typing import ClassVar, Final, cast
|
|
17
|
+
|
|
18
|
+
from .state import (
|
|
19
|
+
ActorRole,
|
|
20
|
+
ActorType,
|
|
21
|
+
ClaimStatus,
|
|
22
|
+
TransitionRequest,
|
|
23
|
+
validate_transition,
|
|
24
|
+
)
|
|
25
|
+
from .types import JsonObject, JsonScalar, JsonValue
|
|
26
|
+
from .validation import ProtocolValidationError, ProtocolValidator
|
|
27
|
+
|
|
28
|
+
PROTOCOL_VERSION: Final = "0.1.0-alpha.3"
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class RecordConstructionError(ValueError):
|
|
32
|
+
"""Raised when typed inputs cannot produce a valid protocol record."""
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class DigestAlgorithm(StrEnum):
|
|
36
|
+
"""Digest algorithms permitted by artifact-reference records."""
|
|
37
|
+
|
|
38
|
+
SHA256 = "sha256"
|
|
39
|
+
SHA512 = "sha512"
|
|
40
|
+
BLAKE3 = "blake3"
|
|
41
|
+
OTHER = "other"
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class AuthorityType(StrEnum):
|
|
45
|
+
"""Declared authority categories for evidence, verdicts, and oracles."""
|
|
46
|
+
|
|
47
|
+
DETERMINISTIC_TOOL = "deterministic_tool"
|
|
48
|
+
DOMAIN_EVALUATOR = "domain_evaluator"
|
|
49
|
+
MODEL_EVALUATOR = "model_evaluator"
|
|
50
|
+
HUMAN_REVIEWER = "human_reviewer"
|
|
51
|
+
POLICY_ENGINE = "policy_engine"
|
|
52
|
+
EXTERNAL_SOURCE = "external_source"
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class ClaimType(StrEnum):
|
|
56
|
+
"""Semantic categories available to claim records."""
|
|
57
|
+
|
|
58
|
+
FACTUAL = "factual"
|
|
59
|
+
CAUSAL = "causal"
|
|
60
|
+
PREDICTIVE = "predictive"
|
|
61
|
+
INTERPRETIVE = "interpretive"
|
|
62
|
+
NORMATIVE = "normative"
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
class PredictionCondition(StrEnum):
|
|
66
|
+
"""Conditions under which a prediction declares its observation."""
|
|
67
|
+
|
|
68
|
+
HYPOTHESIS_TRUE = "hypothesis_true"
|
|
69
|
+
HYPOTHESIS_FALSE = "hypothesis_false"
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
class TestDesign(StrEnum):
|
|
73
|
+
"""Controlled vocabulary for protocol test designs."""
|
|
74
|
+
|
|
75
|
+
__test__: ClassVar[bool] = False
|
|
76
|
+
|
|
77
|
+
CONTROLLED_ABLATION = "controlled_ablation"
|
|
78
|
+
FACTORIAL = "factorial"
|
|
79
|
+
DETERMINISTIC_CHECK = "deterministic_check"
|
|
80
|
+
REPLAY = "replay"
|
|
81
|
+
QUERY = "query"
|
|
82
|
+
SIMULATION = "simulation"
|
|
83
|
+
SOURCE_CORROBORATION = "source_corroboration"
|
|
84
|
+
HUMAN_REVIEW = "human_review"
|
|
85
|
+
OTHER = "other"
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
class TestStatus(StrEnum):
|
|
89
|
+
"""Lifecycle states for immutable planned and executed test records."""
|
|
90
|
+
|
|
91
|
+
__test__: ClassVar[bool] = False
|
|
92
|
+
|
|
93
|
+
PLANNED = "planned"
|
|
94
|
+
RUNNING = "running"
|
|
95
|
+
COMPLETED = "completed"
|
|
96
|
+
FAILED = "failed"
|
|
97
|
+
UNAVAILABLE = "unavailable"
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
class EvidenceRelationType(StrEnum):
|
|
101
|
+
"""Permitted evidence-to-subject relations."""
|
|
102
|
+
|
|
103
|
+
SUPPORTS = "supports"
|
|
104
|
+
CONTRADICTS = "contradicts"
|
|
105
|
+
CONTEXTUALIZES = "contextualizes"
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
class EvidenceType(StrEnum):
|
|
109
|
+
"""Source and production categories for evidence records."""
|
|
110
|
+
|
|
111
|
+
OBSERVATION = "observation"
|
|
112
|
+
DETERMINISTIC_TEST = "deterministic_test"
|
|
113
|
+
BENCHMARK_RESULT = "benchmark_result"
|
|
114
|
+
CALCULATION = "calculation"
|
|
115
|
+
DATABASE_QUERY = "database_query"
|
|
116
|
+
SIMULATION = "simulation"
|
|
117
|
+
SOURCE_RECORD = "source_record"
|
|
118
|
+
MODEL_EVALUATION = "model_evaluation"
|
|
119
|
+
HUMAN_REVIEW = "human_review"
|
|
120
|
+
POLICY_CHECK = "policy_check"
|
|
121
|
+
OTHER = "other"
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
class VerdictOutcome(StrEnum):
|
|
125
|
+
"""Scoped outcomes available to verdict records."""
|
|
126
|
+
|
|
127
|
+
SUPPORTED = "supported"
|
|
128
|
+
REFUTED = "refuted"
|
|
129
|
+
INCONCLUSIVE = "inconclusive"
|
|
130
|
+
MIXED_EVIDENCE = "mixed_evidence"
|
|
131
|
+
CORROBORATED_WITHIN_SCOPE = "corroborated_within_scope"
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
class DecisionDisposition(StrEnum):
|
|
135
|
+
"""Accountable dispositions available to decision records."""
|
|
136
|
+
|
|
137
|
+
APPROVED = "approved"
|
|
138
|
+
REJECTED = "rejected"
|
|
139
|
+
DEFERRED = "deferred"
|
|
140
|
+
ESCALATED = "escalated"
|
|
141
|
+
EXECUTED = "executed"
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def _nonempty(value: str, field_name: str) -> None:
|
|
145
|
+
if not value.strip():
|
|
146
|
+
raise RecordConstructionError(f"{field_name} must not be empty")
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def _timestamp(value: datetime, field_name: str) -> str:
|
|
150
|
+
if value.tzinfo is None or value.utcoffset() is None:
|
|
151
|
+
raise RecordConstructionError(f"{field_name} must be timezone-aware")
|
|
152
|
+
return value.astimezone(UTC).isoformat().replace("+00:00", "Z")
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def _string_values(values: Sequence[str], field_name: str) -> list[JsonValue]:
|
|
156
|
+
# A str is itself a Sequence[str], so type checkers accept one here.
|
|
157
|
+
if isinstance(values, str):
|
|
158
|
+
raise RecordConstructionError(
|
|
159
|
+
f"{field_name} must be a sequence of strings, not a single string"
|
|
160
|
+
)
|
|
161
|
+
return [value for value in values]
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def _object_values(values: Sequence[JsonObject]) -> list[JsonValue]:
|
|
165
|
+
return [value for value in values]
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def _empty_dimensions() -> Mapping[str, JsonScalar]:
|
|
169
|
+
return {}
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
@cache
|
|
173
|
+
def _validator() -> ProtocolValidator:
|
|
174
|
+
return ProtocolValidator()
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def _validated_record(record: JsonObject) -> JsonObject:
|
|
178
|
+
try:
|
|
179
|
+
encoded = json.dumps(
|
|
180
|
+
record,
|
|
181
|
+
allow_nan=False,
|
|
182
|
+
ensure_ascii=False,
|
|
183
|
+
separators=(",", ":"),
|
|
184
|
+
sort_keys=True,
|
|
185
|
+
)
|
|
186
|
+
value = cast(JsonValue, json.loads(encoded))
|
|
187
|
+
except (TypeError, ValueError) as error:
|
|
188
|
+
raise RecordConstructionError(
|
|
189
|
+
f"record contains a value that cannot be represented as strict JSON: {error}"
|
|
190
|
+
) from error
|
|
191
|
+
if not isinstance(value, dict):
|
|
192
|
+
raise TypeError("constructed protocol record was not an object")
|
|
193
|
+
try:
|
|
194
|
+
_validator().validate(value)
|
|
195
|
+
except ProtocolValidationError as error:
|
|
196
|
+
raise RecordConstructionError(str(error)) from error
|
|
197
|
+
return value
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
@dataclass(frozen=True, slots=True)
|
|
201
|
+
class Actor:
|
|
202
|
+
"""A typed actor declaration used in provenance and authorization fields."""
|
|
203
|
+
|
|
204
|
+
actor_id: str
|
|
205
|
+
actor_type: ActorType
|
|
206
|
+
role: ActorRole
|
|
207
|
+
implementation_ref: str | None = None
|
|
208
|
+
|
|
209
|
+
def __post_init__(self) -> None:
|
|
210
|
+
_nonempty(self.actor_id, "actor_id")
|
|
211
|
+
try:
|
|
212
|
+
object.__setattr__(self, "actor_type", ActorType(self.actor_type))
|
|
213
|
+
object.__setattr__(self, "role", ActorRole(self.role))
|
|
214
|
+
except ValueError as error:
|
|
215
|
+
raise RecordConstructionError(str(error)) from error
|
|
216
|
+
if self.implementation_ref is not None:
|
|
217
|
+
_nonempty(self.implementation_ref, "implementation_ref")
|
|
218
|
+
|
|
219
|
+
def to_json_object(self) -> JsonObject:
|
|
220
|
+
"""Return the actor's protocol wire representation."""
|
|
221
|
+
|
|
222
|
+
value: JsonObject = {
|
|
223
|
+
"id": self.actor_id,
|
|
224
|
+
"actor_type": self.actor_type.value,
|
|
225
|
+
"role": self.role.value,
|
|
226
|
+
}
|
|
227
|
+
if self.implementation_ref is not None:
|
|
228
|
+
value["implementation_ref"] = self.implementation_ref
|
|
229
|
+
return value
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
@dataclass(frozen=True, slots=True)
|
|
233
|
+
class Scope:
|
|
234
|
+
"""The explicit boundary within which a record may be interpreted."""
|
|
235
|
+
|
|
236
|
+
description: str
|
|
237
|
+
dimensions: Mapping[str, JsonScalar] = field(default_factory=_empty_dimensions)
|
|
238
|
+
valid_from: datetime | None = None
|
|
239
|
+
valid_until: datetime | None = None
|
|
240
|
+
|
|
241
|
+
def __post_init__(self) -> None:
|
|
242
|
+
_nonempty(self.description, "scope.description")
|
|
243
|
+
object.__setattr__(
|
|
244
|
+
self,
|
|
245
|
+
"dimensions",
|
|
246
|
+
MappingProxyType(dict(self.dimensions)),
|
|
247
|
+
)
|
|
248
|
+
if self.valid_from is not None:
|
|
249
|
+
_timestamp(self.valid_from, "scope.valid_from")
|
|
250
|
+
if self.valid_until is not None:
|
|
251
|
+
_timestamp(self.valid_until, "scope.valid_until")
|
|
252
|
+
|
|
253
|
+
def to_json_object(self) -> JsonObject:
|
|
254
|
+
"""Return the scope's protocol wire representation."""
|
|
255
|
+
|
|
256
|
+
value: JsonObject = {"description": self.description}
|
|
257
|
+
if self.dimensions:
|
|
258
|
+
value["dimensions"] = dict(self.dimensions)
|
|
259
|
+
if self.valid_from is not None:
|
|
260
|
+
value["valid_from"] = _timestamp(self.valid_from, "scope.valid_from")
|
|
261
|
+
if self.valid_until is not None:
|
|
262
|
+
value["valid_until"] = _timestamp(
|
|
263
|
+
self.valid_until,
|
|
264
|
+
"scope.valid_until",
|
|
265
|
+
)
|
|
266
|
+
return value
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
@dataclass(frozen=True, slots=True)
|
|
270
|
+
class RecordHeader:
|
|
271
|
+
"""Shared provenance fields for one immutable protocol record."""
|
|
272
|
+
|
|
273
|
+
record_id: str
|
|
274
|
+
created_at: datetime
|
|
275
|
+
created_by: Actor
|
|
276
|
+
|
|
277
|
+
def __post_init__(self) -> None:
|
|
278
|
+
_nonempty(self.record_id, "record_id")
|
|
279
|
+
_timestamp(self.created_at, "created_at")
|
|
280
|
+
|
|
281
|
+
def to_json_object(self) -> JsonObject:
|
|
282
|
+
"""Return the shared protocol record header fields."""
|
|
283
|
+
|
|
284
|
+
return {
|
|
285
|
+
"protocol_version": PROTOCOL_VERSION,
|
|
286
|
+
"id": self.record_id,
|
|
287
|
+
"created_at": _timestamp(self.created_at, "created_at"),
|
|
288
|
+
"created_by": self.created_by.to_json_object(),
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
@dataclass(frozen=True, slots=True)
|
|
293
|
+
class Digest:
|
|
294
|
+
"""A declared digest for an externally retained artifact."""
|
|
295
|
+
|
|
296
|
+
algorithm: DigestAlgorithm
|
|
297
|
+
value: str
|
|
298
|
+
|
|
299
|
+
def __post_init__(self) -> None:
|
|
300
|
+
_nonempty(self.value, "digest.value")
|
|
301
|
+
|
|
302
|
+
def to_json_object(self) -> JsonObject:
|
|
303
|
+
"""Return the digest's protocol wire representation."""
|
|
304
|
+
|
|
305
|
+
return {
|
|
306
|
+
"algorithm": self.algorithm.value,
|
|
307
|
+
"value": self.value,
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
@dataclass(frozen=True, slots=True)
|
|
312
|
+
class Authority:
|
|
313
|
+
"""The declared issuer and basis of evidence or a verdict."""
|
|
314
|
+
|
|
315
|
+
authority_type: AuthorityType
|
|
316
|
+
actor_ref: str
|
|
317
|
+
basis: str
|
|
318
|
+
independent_of_subject: bool | None = None
|
|
319
|
+
|
|
320
|
+
def __post_init__(self) -> None:
|
|
321
|
+
_nonempty(self.actor_ref, "authority.actor_ref")
|
|
322
|
+
_nonempty(self.basis, "authority.basis")
|
|
323
|
+
|
|
324
|
+
def to_json_object(self) -> JsonObject:
|
|
325
|
+
"""Return the authority's protocol wire representation."""
|
|
326
|
+
|
|
327
|
+
value: JsonObject = {
|
|
328
|
+
"authority_type": self.authority_type.value,
|
|
329
|
+
"actor_ref": self.actor_ref,
|
|
330
|
+
"basis": self.basis,
|
|
331
|
+
}
|
|
332
|
+
if self.independent_of_subject is not None:
|
|
333
|
+
value["independent_of_subject"] = self.independent_of_subject
|
|
334
|
+
return value
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
@dataclass(frozen=True, slots=True)
|
|
338
|
+
class Oracle:
|
|
339
|
+
"""A declared test oracle or evaluator adapter."""
|
|
340
|
+
|
|
341
|
+
adapter: str
|
|
342
|
+
version: str
|
|
343
|
+
authority_type: AuthorityType
|
|
344
|
+
declared_scope: str | None = None
|
|
345
|
+
|
|
346
|
+
def __post_init__(self) -> None:
|
|
347
|
+
_nonempty(self.adapter, "oracle.adapter")
|
|
348
|
+
_nonempty(self.version, "oracle.version")
|
|
349
|
+
if self.declared_scope is not None:
|
|
350
|
+
_nonempty(self.declared_scope, "oracle.declared_scope")
|
|
351
|
+
|
|
352
|
+
def to_json_object(self) -> JsonObject:
|
|
353
|
+
"""Return the oracle's protocol wire representation."""
|
|
354
|
+
|
|
355
|
+
value: JsonObject = {
|
|
356
|
+
"adapter": self.adapter,
|
|
357
|
+
"version": self.version,
|
|
358
|
+
"authority_type": self.authority_type.value,
|
|
359
|
+
}
|
|
360
|
+
if self.declared_scope is not None:
|
|
361
|
+
value["declared_scope"] = self.declared_scope
|
|
362
|
+
return value
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
@dataclass(frozen=True, slots=True)
|
|
366
|
+
class EvidenceRelation:
|
|
367
|
+
"""A scoped relation between one evidence record and one subject."""
|
|
368
|
+
|
|
369
|
+
subject_ref: str
|
|
370
|
+
relation: EvidenceRelationType
|
|
371
|
+
public_note: str | None = None
|
|
372
|
+
|
|
373
|
+
def __post_init__(self) -> None:
|
|
374
|
+
_nonempty(self.subject_ref, "evidence_relation.subject_ref")
|
|
375
|
+
if self.public_note is not None:
|
|
376
|
+
_nonempty(self.public_note, "evidence_relation.public_note")
|
|
377
|
+
|
|
378
|
+
def to_json_object(self) -> JsonObject:
|
|
379
|
+
"""Return the evidence relation's protocol wire representation."""
|
|
380
|
+
|
|
381
|
+
value: JsonObject = {
|
|
382
|
+
"subject_ref": self.subject_ref,
|
|
383
|
+
"relation": self.relation.value,
|
|
384
|
+
}
|
|
385
|
+
if self.public_note is not None:
|
|
386
|
+
value["public_note"] = self.public_note
|
|
387
|
+
return value
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
@dataclass(frozen=True, slots=True)
|
|
391
|
+
class TestCost:
|
|
392
|
+
"""Optional observable resource use for one test execution."""
|
|
393
|
+
|
|
394
|
+
__test__: ClassVar[bool] = False
|
|
395
|
+
|
|
396
|
+
model_tokens: int | None = None
|
|
397
|
+
wall_time_ms: int | None = None
|
|
398
|
+
human_minutes: float | None = None
|
|
399
|
+
|
|
400
|
+
def to_json_object(self) -> JsonObject:
|
|
401
|
+
"""Return only the declared test-cost fields."""
|
|
402
|
+
|
|
403
|
+
value: JsonObject = {}
|
|
404
|
+
if self.model_tokens is not None:
|
|
405
|
+
value["model_tokens"] = self.model_tokens
|
|
406
|
+
if self.wall_time_ms is not None:
|
|
407
|
+
value["wall_time_ms"] = self.wall_time_ms
|
|
408
|
+
if self.human_minutes is not None:
|
|
409
|
+
value["human_minutes"] = self.human_minutes
|
|
410
|
+
return value
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
def _record(header: RecordHeader, kind: str) -> JsonObject:
|
|
414
|
+
value = header.to_json_object()
|
|
415
|
+
value["kind"] = kind
|
|
416
|
+
return value
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
def artifact_reference_record(
|
|
420
|
+
header: RecordHeader,
|
|
421
|
+
*,
|
|
422
|
+
uri: str,
|
|
423
|
+
media_type: str,
|
|
424
|
+
title: str | None = None,
|
|
425
|
+
digest: Digest | None = None,
|
|
426
|
+
captured_at: datetime | None = None,
|
|
427
|
+
derived_from_refs: Sequence[str] = (),
|
|
428
|
+
) -> JsonObject:
|
|
429
|
+
"""Construct and schema-validate one artifact-reference record."""
|
|
430
|
+
|
|
431
|
+
value = _record(header, "artifact_reference")
|
|
432
|
+
value["uri"] = uri
|
|
433
|
+
value["media_type"] = media_type
|
|
434
|
+
if title is not None:
|
|
435
|
+
value["title"] = title
|
|
436
|
+
if digest is not None:
|
|
437
|
+
value["digest"] = digest.to_json_object()
|
|
438
|
+
if captured_at is not None:
|
|
439
|
+
value["captured_at"] = _timestamp(captured_at, "captured_at")
|
|
440
|
+
if derived_from_refs:
|
|
441
|
+
value["derived_from_refs"] = _string_values(
|
|
442
|
+
derived_from_refs, "derived_from_refs"
|
|
443
|
+
)
|
|
444
|
+
return _validated_record(value)
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
def claim_record(
|
|
448
|
+
header: RecordHeader,
|
|
449
|
+
*,
|
|
450
|
+
proposition: str,
|
|
451
|
+
claim_type: ClaimType,
|
|
452
|
+
scope: Scope,
|
|
453
|
+
status: ClaimStatus = ClaimStatus.PROPOSED,
|
|
454
|
+
evidence_refs: Sequence[str] = (),
|
|
455
|
+
contradicting_evidence_refs: Sequence[str] = (),
|
|
456
|
+
dependency_refs: Sequence[str] = (),
|
|
457
|
+
transition_refs: Sequence[str] = (),
|
|
458
|
+
invalidation_conditions: Sequence[str] = (),
|
|
459
|
+
) -> JsonObject:
|
|
460
|
+
"""Construct and schema-validate one claim record."""
|
|
461
|
+
|
|
462
|
+
value = _record(header, "claim")
|
|
463
|
+
value.update(
|
|
464
|
+
{
|
|
465
|
+
"proposition": proposition,
|
|
466
|
+
"claim_type": claim_type.value,
|
|
467
|
+
"status": status.value,
|
|
468
|
+
"scope": scope.to_json_object(),
|
|
469
|
+
}
|
|
470
|
+
)
|
|
471
|
+
for field_name, references in (
|
|
472
|
+
("evidence_refs", evidence_refs),
|
|
473
|
+
("contradicting_evidence_refs", contradicting_evidence_refs),
|
|
474
|
+
("dependency_refs", dependency_refs),
|
|
475
|
+
("transition_refs", transition_refs),
|
|
476
|
+
("invalidation_conditions", invalidation_conditions),
|
|
477
|
+
):
|
|
478
|
+
if references:
|
|
479
|
+
value[field_name] = _string_values(references, field_name)
|
|
480
|
+
return _validated_record(value)
|
|
481
|
+
|
|
482
|
+
|
|
483
|
+
def hypothesis_record(
|
|
484
|
+
header: RecordHeader,
|
|
485
|
+
*,
|
|
486
|
+
statement: str,
|
|
487
|
+
scope: Scope,
|
|
488
|
+
status: ClaimStatus = ClaimStatus.PROPOSED,
|
|
489
|
+
explains_evidence_refs: Sequence[str] = (),
|
|
490
|
+
alternative_hypothesis_refs: Sequence[str] = (),
|
|
491
|
+
prediction_refs: Sequence[str] = (),
|
|
492
|
+
dependency_refs: Sequence[str] = (),
|
|
493
|
+
) -> JsonObject:
|
|
494
|
+
"""Construct and schema-validate one hypothesis record."""
|
|
495
|
+
|
|
496
|
+
value = _record(header, "hypothesis")
|
|
497
|
+
value.update(
|
|
498
|
+
{
|
|
499
|
+
"statement": statement,
|
|
500
|
+
"status": status.value,
|
|
501
|
+
"scope": scope.to_json_object(),
|
|
502
|
+
}
|
|
503
|
+
)
|
|
504
|
+
for field_name, references in (
|
|
505
|
+
("explains_evidence_refs", explains_evidence_refs),
|
|
506
|
+
("alternative_hypothesis_refs", alternative_hypothesis_refs),
|
|
507
|
+
("prediction_refs", prediction_refs),
|
|
508
|
+
("dependency_refs", dependency_refs),
|
|
509
|
+
):
|
|
510
|
+
if references:
|
|
511
|
+
value[field_name] = _string_values(references, field_name)
|
|
512
|
+
return _validated_record(value)
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
def prediction_record(
|
|
516
|
+
header: RecordHeader,
|
|
517
|
+
*,
|
|
518
|
+
hypothesis_ref: str,
|
|
519
|
+
condition: PredictionCondition,
|
|
520
|
+
expected_observation: str,
|
|
521
|
+
scope: Scope,
|
|
522
|
+
falsified_when: str | None = None,
|
|
523
|
+
test_ref: str | None = None,
|
|
524
|
+
) -> JsonObject:
|
|
525
|
+
"""Construct and schema-validate one falsifiable prediction record."""
|
|
526
|
+
|
|
527
|
+
value = _record(header, "prediction")
|
|
528
|
+
value.update(
|
|
529
|
+
{
|
|
530
|
+
"hypothesis_ref": hypothesis_ref,
|
|
531
|
+
"condition": condition.value,
|
|
532
|
+
"expected_observation": expected_observation,
|
|
533
|
+
"scope": scope.to_json_object(),
|
|
534
|
+
}
|
|
535
|
+
)
|
|
536
|
+
if falsified_when is not None:
|
|
537
|
+
value["falsified_when"] = falsified_when
|
|
538
|
+
if test_ref is not None:
|
|
539
|
+
value["test_ref"] = test_ref
|
|
540
|
+
return _validated_record(value)
|
|
541
|
+
|
|
542
|
+
|
|
543
|
+
def protocol_test_record(
|
|
544
|
+
header: RecordHeader,
|
|
545
|
+
*,
|
|
546
|
+
question: str,
|
|
547
|
+
design: TestDesign,
|
|
548
|
+
status: TestStatus,
|
|
549
|
+
oracle: Oracle,
|
|
550
|
+
scope: Scope,
|
|
551
|
+
subject_refs: Sequence[str] = (),
|
|
552
|
+
prediction_refs: Sequence[str] = (),
|
|
553
|
+
plan_ref: str | None = None,
|
|
554
|
+
evidence_refs: Sequence[str] = (),
|
|
555
|
+
cost: TestCost | None = None,
|
|
556
|
+
) -> JsonObject:
|
|
557
|
+
"""Construct and schema-validate one planned or executed test record.
|
|
558
|
+
|
|
559
|
+
A ``TestStatus.COMPLETED`` test must cite the evidence it produced in
|
|
560
|
+
``evidence_refs`` and should name its plan in ``plan_ref``. The evidence
|
|
561
|
+
usually cites this test back through ``test_ref``, so append the two
|
|
562
|
+
records in one ``Ledger.extend`` or ``JsonlLedgerStore.extend`` batch.
|
|
563
|
+
"""
|
|
564
|
+
|
|
565
|
+
value = _record(header, "test")
|
|
566
|
+
value.update(
|
|
567
|
+
{
|
|
568
|
+
"question": question,
|
|
569
|
+
"design": design.value,
|
|
570
|
+
"status": status.value,
|
|
571
|
+
"oracle": oracle.to_json_object(),
|
|
572
|
+
"scope": scope.to_json_object(),
|
|
573
|
+
}
|
|
574
|
+
)
|
|
575
|
+
if subject_refs:
|
|
576
|
+
value["subject_refs"] = _string_values(subject_refs, "subject_refs")
|
|
577
|
+
if prediction_refs:
|
|
578
|
+
value["prediction_refs"] = _string_values(prediction_refs, "prediction_refs")
|
|
579
|
+
if plan_ref is not None:
|
|
580
|
+
value["plan_ref"] = plan_ref
|
|
581
|
+
if evidence_refs:
|
|
582
|
+
value["evidence_refs"] = _string_values(evidence_refs, "evidence_refs")
|
|
583
|
+
if cost is not None:
|
|
584
|
+
value["cost"] = cost.to_json_object()
|
|
585
|
+
return _validated_record(value)
|
|
586
|
+
|
|
587
|
+
|
|
588
|
+
def evidence_record(
|
|
589
|
+
header: RecordHeader,
|
|
590
|
+
*,
|
|
591
|
+
evidence_type: EvidenceType,
|
|
592
|
+
relations: Sequence[EvidenceRelation],
|
|
593
|
+
authority: Authority,
|
|
594
|
+
scope: Scope,
|
|
595
|
+
artifact_refs: Sequence[str] = (),
|
|
596
|
+
test_ref: str | None = None,
|
|
597
|
+
result: Mapping[str, JsonValue] | None = None,
|
|
598
|
+
expires_at: datetime | None = None,
|
|
599
|
+
) -> JsonObject:
|
|
600
|
+
"""Construct and schema-validate one evidence record."""
|
|
601
|
+
|
|
602
|
+
value = _record(header, "evidence")
|
|
603
|
+
value.update(
|
|
604
|
+
{
|
|
605
|
+
"evidence_type": evidence_type.value,
|
|
606
|
+
"relations": _object_values(
|
|
607
|
+
tuple(relation.to_json_object() for relation in relations)
|
|
608
|
+
),
|
|
609
|
+
"authority": authority.to_json_object(),
|
|
610
|
+
"scope": scope.to_json_object(),
|
|
611
|
+
}
|
|
612
|
+
)
|
|
613
|
+
if artifact_refs:
|
|
614
|
+
value["artifact_refs"] = _string_values(artifact_refs, "artifact_refs")
|
|
615
|
+
if test_ref is not None:
|
|
616
|
+
value["test_ref"] = test_ref
|
|
617
|
+
if result is not None:
|
|
618
|
+
value["result"] = deepcopy(dict(result))
|
|
619
|
+
if expires_at is not None:
|
|
620
|
+
value["expires_at"] = _timestamp(expires_at, "expires_at")
|
|
621
|
+
return _validated_record(value)
|
|
622
|
+
|
|
623
|
+
|
|
624
|
+
def verdict_record(
|
|
625
|
+
header: RecordHeader,
|
|
626
|
+
*,
|
|
627
|
+
subject_ref: str,
|
|
628
|
+
outcome: VerdictOutcome,
|
|
629
|
+
evidence_refs: Sequence[str],
|
|
630
|
+
authority: Authority,
|
|
631
|
+
scope: Scope,
|
|
632
|
+
public_rationale: str,
|
|
633
|
+
policy_ref: str | None = None,
|
|
634
|
+
) -> JsonObject:
|
|
635
|
+
"""Construct and schema-validate one evidence-bound verdict record."""
|
|
636
|
+
|
|
637
|
+
value = _record(header, "verdict")
|
|
638
|
+
value.update(
|
|
639
|
+
{
|
|
640
|
+
"subject_ref": subject_ref,
|
|
641
|
+
"outcome": outcome.value,
|
|
642
|
+
"evidence_refs": _string_values(evidence_refs, "evidence_refs"),
|
|
643
|
+
"authority": authority.to_json_object(),
|
|
644
|
+
"scope": scope.to_json_object(),
|
|
645
|
+
"public_rationale": public_rationale,
|
|
646
|
+
}
|
|
647
|
+
)
|
|
648
|
+
if policy_ref is not None:
|
|
649
|
+
value["policy_ref"] = policy_ref
|
|
650
|
+
return _validated_record(value)
|
|
651
|
+
|
|
652
|
+
|
|
653
|
+
def decision_record(
|
|
654
|
+
header: RecordHeader,
|
|
655
|
+
*,
|
|
656
|
+
question: str,
|
|
657
|
+
disposition: DecisionDisposition,
|
|
658
|
+
authorized_by: Actor,
|
|
659
|
+
scope: Scope,
|
|
660
|
+
policy_ref: str | None = None,
|
|
661
|
+
relied_on_claim_refs: Sequence[str] = (),
|
|
662
|
+
unresolved_claim_refs: Sequence[str] = (),
|
|
663
|
+
reconsider_when: Sequence[str] = (),
|
|
664
|
+
public_rationale: str | None = None,
|
|
665
|
+
) -> JsonObject:
|
|
666
|
+
"""Construct and schema-validate one accountable decision record."""
|
|
667
|
+
|
|
668
|
+
value = _record(header, "decision")
|
|
669
|
+
value.update(
|
|
670
|
+
{
|
|
671
|
+
"question": question,
|
|
672
|
+
"disposition": disposition.value,
|
|
673
|
+
"authorized_by": authorized_by.to_json_object(),
|
|
674
|
+
"scope": scope.to_json_object(),
|
|
675
|
+
}
|
|
676
|
+
)
|
|
677
|
+
if policy_ref is not None:
|
|
678
|
+
value["policy_ref"] = policy_ref
|
|
679
|
+
if relied_on_claim_refs:
|
|
680
|
+
value["relied_on_claim_refs"] = _string_values(
|
|
681
|
+
relied_on_claim_refs, "relied_on_claim_refs"
|
|
682
|
+
)
|
|
683
|
+
if unresolved_claim_refs:
|
|
684
|
+
value["unresolved_claim_refs"] = _string_values(
|
|
685
|
+
unresolved_claim_refs, "unresolved_claim_refs"
|
|
686
|
+
)
|
|
687
|
+
if reconsider_when:
|
|
688
|
+
value["reconsider_when"] = _string_values(reconsider_when, "reconsider_when")
|
|
689
|
+
if public_rationale is not None:
|
|
690
|
+
value["public_rationale"] = public_rationale
|
|
691
|
+
return _validated_record(value)
|
|
692
|
+
|
|
693
|
+
|
|
694
|
+
def status_transition_record(
|
|
695
|
+
header: RecordHeader,
|
|
696
|
+
*,
|
|
697
|
+
subject_ref: str,
|
|
698
|
+
from_status: ClaimStatus,
|
|
699
|
+
to_status: ClaimStatus,
|
|
700
|
+
authorized_by: Actor,
|
|
701
|
+
reason: str,
|
|
702
|
+
evidence_refs: Sequence[str] = (),
|
|
703
|
+
verdict_ref: str | None = None,
|
|
704
|
+
policy_ref: str | None = None,
|
|
705
|
+
) -> JsonObject:
|
|
706
|
+
"""Construct and validate one transition under the reference state policy."""
|
|
707
|
+
|
|
708
|
+
evidence = _string_values(evidence_refs, "evidence_refs")
|
|
709
|
+
try:
|
|
710
|
+
# The request converts plain status strings to enum members and
|
|
711
|
+
# rejects unknown ones, and the record uses its converted values.
|
|
712
|
+
request = TransitionRequest(
|
|
713
|
+
subject_ref=subject_ref,
|
|
714
|
+
from_status=from_status,
|
|
715
|
+
to_status=to_status,
|
|
716
|
+
authorized_by=authorized_by.actor_id,
|
|
717
|
+
actor_type=authorized_by.actor_type,
|
|
718
|
+
actor_role=authorized_by.role,
|
|
719
|
+
evidence_refs=tuple(cast(list[str], evidence)),
|
|
720
|
+
verdict_ref=verdict_ref,
|
|
721
|
+
policy_ref=policy_ref,
|
|
722
|
+
)
|
|
723
|
+
validate_transition(request)
|
|
724
|
+
except ValueError as error:
|
|
725
|
+
raise RecordConstructionError(str(error)) from error
|
|
726
|
+
|
|
727
|
+
value = _record(header, "status_transition")
|
|
728
|
+
value.update(
|
|
729
|
+
{
|
|
730
|
+
"subject_ref": subject_ref,
|
|
731
|
+
"from_status": request.from_status.value,
|
|
732
|
+
"to_status": request.to_status.value,
|
|
733
|
+
"authorized_by": authorized_by.to_json_object(),
|
|
734
|
+
"reason": reason,
|
|
735
|
+
}
|
|
736
|
+
)
|
|
737
|
+
if evidence:
|
|
738
|
+
value["evidence_refs"] = evidence
|
|
739
|
+
if verdict_ref is not None:
|
|
740
|
+
value["verdict_ref"] = verdict_ref
|
|
741
|
+
if policy_ref is not None:
|
|
742
|
+
value["policy_ref"] = policy_ref
|
|
743
|
+
return _validated_record(value)
|