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/reporting.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
# Copyright (C) 2026 Greater Expanse LLC
|
|
3
|
+
|
|
4
|
+
"""Structured, provider-neutral projections of validated ledger state."""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
from collections import Counter
|
|
9
|
+
from collections.abc import Mapping
|
|
10
|
+
from dataclasses import dataclass
|
|
11
|
+
from enum import StrEnum
|
|
12
|
+
from types import MappingProxyType
|
|
13
|
+
|
|
14
|
+
from .ledger import Ledger
|
|
15
|
+
from .state import ClaimStatus
|
|
16
|
+
from .types import JsonObject
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class SubjectKind(StrEnum):
|
|
20
|
+
"""Record kinds whose epistemic state can be replayed."""
|
|
21
|
+
|
|
22
|
+
CLAIM = "claim"
|
|
23
|
+
HYPOTHESIS = "hypothesis"
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@dataclass(frozen=True, slots=True)
|
|
27
|
+
class SubjectReplay:
|
|
28
|
+
"""Initial and replayed state for one claim or hypothesis."""
|
|
29
|
+
|
|
30
|
+
subject_id: str
|
|
31
|
+
kind: SubjectKind
|
|
32
|
+
initial_status: ClaimStatus
|
|
33
|
+
current_status: ClaimStatus
|
|
34
|
+
transition_count: int
|
|
35
|
+
|
|
36
|
+
def to_json_object(self) -> JsonObject:
|
|
37
|
+
"""Return a JSON-compatible structured representation."""
|
|
38
|
+
|
|
39
|
+
return {
|
|
40
|
+
"subject_id": self.subject_id,
|
|
41
|
+
"kind": self.kind.value,
|
|
42
|
+
"initial_status": self.initial_status.value,
|
|
43
|
+
"current_status": self.current_status.value,
|
|
44
|
+
"transition_count": self.transition_count,
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
@dataclass(frozen=True, slots=True)
|
|
49
|
+
class LedgerSummary:
|
|
50
|
+
"""Aggregate record and current-state counts for one valid ledger."""
|
|
51
|
+
|
|
52
|
+
record_count: int
|
|
53
|
+
record_counts: Mapping[str, int]
|
|
54
|
+
state_counts: Mapping[ClaimStatus, int]
|
|
55
|
+
subjects: tuple[SubjectReplay, ...]
|
|
56
|
+
|
|
57
|
+
@property
|
|
58
|
+
def subject_count(self) -> int:
|
|
59
|
+
"""Return the number of replayable claims and hypotheses."""
|
|
60
|
+
|
|
61
|
+
return len(self.subjects)
|
|
62
|
+
|
|
63
|
+
def to_json_object(self) -> JsonObject:
|
|
64
|
+
"""Return a JSON-compatible structured representation."""
|
|
65
|
+
|
|
66
|
+
return {
|
|
67
|
+
"record_count": self.record_count,
|
|
68
|
+
"subject_count": self.subject_count,
|
|
69
|
+
"record_counts": dict(self.record_counts),
|
|
70
|
+
"state_counts": {
|
|
71
|
+
status.value: count for status, count in self.state_counts.items()
|
|
72
|
+
},
|
|
73
|
+
"subjects": [subject.to_json_object() for subject in self.subjects],
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def _string(record: JsonObject, field: str) -> str:
|
|
78
|
+
value = record[field]
|
|
79
|
+
if not isinstance(value, str):
|
|
80
|
+
raise TypeError(f"validated field {field!r} was not a string")
|
|
81
|
+
return value
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def replay_subjects(ledger: Ledger) -> tuple[SubjectReplay, ...]:
|
|
85
|
+
"""Project claims and hypotheses in base-record order after state replay."""
|
|
86
|
+
|
|
87
|
+
records = ledger.records
|
|
88
|
+
transition_counts = Counter(
|
|
89
|
+
_string(record, "subject_ref")
|
|
90
|
+
for record in records
|
|
91
|
+
if _string(record, "kind") == "status_transition"
|
|
92
|
+
)
|
|
93
|
+
subjects: list[SubjectReplay] = []
|
|
94
|
+
for record in records:
|
|
95
|
+
kind_value = _string(record, "kind")
|
|
96
|
+
if kind_value not in {SubjectKind.CLAIM.value, SubjectKind.HYPOTHESIS.value}:
|
|
97
|
+
continue
|
|
98
|
+
subject_id = _string(record, "id")
|
|
99
|
+
subjects.append(
|
|
100
|
+
SubjectReplay(
|
|
101
|
+
subject_id=subject_id,
|
|
102
|
+
kind=SubjectKind(kind_value),
|
|
103
|
+
initial_status=ClaimStatus(_string(record, "status")),
|
|
104
|
+
current_status=ledger.snapshot.current_states[subject_id],
|
|
105
|
+
transition_count=transition_counts[subject_id],
|
|
106
|
+
)
|
|
107
|
+
)
|
|
108
|
+
return tuple(subjects)
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def summarize_ledger(ledger: Ledger) -> LedgerSummary:
|
|
112
|
+
"""Return deterministic aggregate counts and subject replay projections."""
|
|
113
|
+
|
|
114
|
+
records = ledger.records
|
|
115
|
+
subjects = replay_subjects(ledger)
|
|
116
|
+
record_counts = Counter(_string(record, "kind") for record in records)
|
|
117
|
+
state_counts = Counter(subject.current_status for subject in subjects)
|
|
118
|
+
return LedgerSummary(
|
|
119
|
+
record_count=len(records),
|
|
120
|
+
record_counts=MappingProxyType(dict(sorted(record_counts.items()))),
|
|
121
|
+
state_counts=MappingProxyType(
|
|
122
|
+
dict(sorted(state_counts.items(), key=lambda item: item[0].value))
|
|
123
|
+
),
|
|
124
|
+
subjects=subjects,
|
|
125
|
+
)
|
itself/schema_export.py
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
# Copyright (C) 2026 Greater Expanse LLC
|
|
3
|
+
|
|
4
|
+
"""Deterministic exports for Itself's canonical JSON Schemas."""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import hashlib
|
|
9
|
+
import json
|
|
10
|
+
import shutil
|
|
11
|
+
import tempfile
|
|
12
|
+
from dataclasses import dataclass
|
|
13
|
+
from importlib.resources import files
|
|
14
|
+
from pathlib import Path
|
|
15
|
+
from typing import Final, cast
|
|
16
|
+
|
|
17
|
+
from ._filesystem import publish_path_no_replace
|
|
18
|
+
from .types import JsonObject, JsonValue, StrPath
|
|
19
|
+
|
|
20
|
+
SCHEMA_CATALOG_VERSION: Final = "1"
|
|
21
|
+
SCHEMA_LINES: Final = ("v0alpha", "v0alpha2")
|
|
22
|
+
SCHEMA_LINE: Final = "v0alpha2"
|
|
23
|
+
SCHEMA_ORIGIN: Final = "https://greaterexpanse.com"
|
|
24
|
+
SCHEMA_PUBLIC_ROOT: Final = f"{SCHEMA_ORIGIN}/itself/schemas/{SCHEMA_LINE}"
|
|
25
|
+
SCHEMA_NAMES: Final = (
|
|
26
|
+
"evidence-bundle.schema.json",
|
|
27
|
+
"protocol.schema.json",
|
|
28
|
+
"reasoning-receipt.schema.json",
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class SchemaExportError(ValueError):
|
|
33
|
+
"""Raised when canonical schemas cannot be exported safely."""
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@dataclass(frozen=True, slots=True)
|
|
37
|
+
class PublishedSchema:
|
|
38
|
+
"""Content identity and public location for one canonical schema."""
|
|
39
|
+
|
|
40
|
+
name: str
|
|
41
|
+
schema_id: str
|
|
42
|
+
title: str
|
|
43
|
+
sha256: str
|
|
44
|
+
relative_path: str
|
|
45
|
+
|
|
46
|
+
def to_json_object(self) -> JsonObject:
|
|
47
|
+
"""Return the language-neutral catalog representation."""
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
"name": self.name,
|
|
51
|
+
"schema_id": self.schema_id,
|
|
52
|
+
"title": self.title,
|
|
53
|
+
"sha256": self.sha256,
|
|
54
|
+
"path": self.relative_path,
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
@dataclass(frozen=True, slots=True)
|
|
59
|
+
class SchemaExport:
|
|
60
|
+
"""One completed deterministic canonical-schema export."""
|
|
61
|
+
|
|
62
|
+
destination: Path
|
|
63
|
+
schemas: tuple[PublishedSchema, ...]
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _canonical_json(value: JsonObject) -> bytes:
|
|
67
|
+
return (
|
|
68
|
+
json.dumps(
|
|
69
|
+
value,
|
|
70
|
+
ensure_ascii=False,
|
|
71
|
+
indent=2,
|
|
72
|
+
sort_keys=True,
|
|
73
|
+
allow_nan=False,
|
|
74
|
+
)
|
|
75
|
+
+ "\n"
|
|
76
|
+
).encode()
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def _schema_object(name: str, content: bytes) -> JsonObject:
|
|
80
|
+
try:
|
|
81
|
+
value = cast(JsonValue, json.loads(content))
|
|
82
|
+
except (UnicodeDecodeError, json.JSONDecodeError) as error:
|
|
83
|
+
raise SchemaExportError(f"{name} is not valid UTF-8 JSON") from error
|
|
84
|
+
if not isinstance(value, dict):
|
|
85
|
+
raise SchemaExportError(f"{name} must contain a JSON object")
|
|
86
|
+
return value
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _published_schema(line: str, name: str, content: bytes) -> PublishedSchema:
|
|
90
|
+
schema = _schema_object(name, content)
|
|
91
|
+
expected_id = f"{SCHEMA_ORIGIN}/itself/schemas/{line}/{name}"
|
|
92
|
+
if schema.get("$id") != expected_id:
|
|
93
|
+
raise SchemaExportError(f"{name} must declare canonical $id {expected_id!r}")
|
|
94
|
+
title = schema.get("title")
|
|
95
|
+
if not isinstance(title, str) or not title:
|
|
96
|
+
raise SchemaExportError(f"{name} must declare a non-empty title")
|
|
97
|
+
return PublishedSchema(
|
|
98
|
+
name=name,
|
|
99
|
+
schema_id=expected_id,
|
|
100
|
+
title=title,
|
|
101
|
+
sha256=hashlib.sha256(content).hexdigest(),
|
|
102
|
+
relative_path=f"itself/schemas/{line}/{name}",
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def _catalog(
|
|
107
|
+
line: str,
|
|
108
|
+
schemas: tuple[PublishedSchema, ...],
|
|
109
|
+
) -> JsonObject:
|
|
110
|
+
return {
|
|
111
|
+
"catalog_version": SCHEMA_CATALOG_VERSION,
|
|
112
|
+
"homepage": "https://greaterexpanse.com/itself",
|
|
113
|
+
"name": "Itself canonical JSON Schemas",
|
|
114
|
+
"schema_line": line,
|
|
115
|
+
"schemas": [schema.to_json_object() for schema in schemas],
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def export_schemas(destination: StrPath) -> SchemaExport:
|
|
120
|
+
"""Export canonical packaged schemas, catalogs, and checksums.
|
|
121
|
+
|
|
122
|
+
The destination must not already exist. It becomes available only after the
|
|
123
|
+
complete export has been written.
|
|
124
|
+
"""
|
|
125
|
+
|
|
126
|
+
destination_path = Path(destination)
|
|
127
|
+
if destination_path.exists() or destination_path.is_symlink():
|
|
128
|
+
raise SchemaExportError(
|
|
129
|
+
f"schema export destination already exists: {destination_path}"
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
destination_path.parent.mkdir(parents=True, exist_ok=True)
|
|
133
|
+
staging = Path(
|
|
134
|
+
tempfile.mkdtemp(
|
|
135
|
+
prefix=f".{destination_path.name}.staging-",
|
|
136
|
+
dir=destination_path.parent,
|
|
137
|
+
)
|
|
138
|
+
)
|
|
139
|
+
try:
|
|
140
|
+
catalog_directory = staging / "itself" / "schemas"
|
|
141
|
+
published: list[PublishedSchema] = []
|
|
142
|
+
current_catalog: bytes | None = None
|
|
143
|
+
for line in SCHEMA_LINES:
|
|
144
|
+
schema_directory = catalog_directory / line
|
|
145
|
+
schema_directory.mkdir(parents=True)
|
|
146
|
+
schema_resources = files("itself").joinpath("schemas", line)
|
|
147
|
+
line_schemas: list[PublishedSchema] = []
|
|
148
|
+
for name in SCHEMA_NAMES:
|
|
149
|
+
content = schema_resources.joinpath(name).read_bytes()
|
|
150
|
+
schema = _published_schema(line, name, content)
|
|
151
|
+
(schema_directory / name).write_bytes(content)
|
|
152
|
+
line_schemas.append(schema)
|
|
153
|
+
published.append(schema)
|
|
154
|
+
|
|
155
|
+
schemas = tuple(line_schemas)
|
|
156
|
+
catalog = _canonical_json(_catalog(line, schemas))
|
|
157
|
+
(schema_directory / "index.json").write_bytes(catalog)
|
|
158
|
+
(schema_directory / "SHA256SUMS").write_text(
|
|
159
|
+
"".join(f"{schema.sha256} {schema.name}\n" for schema in schemas),
|
|
160
|
+
encoding="utf-8",
|
|
161
|
+
)
|
|
162
|
+
if line == SCHEMA_LINE:
|
|
163
|
+
current_catalog = catalog
|
|
164
|
+
if current_catalog is None:
|
|
165
|
+
raise RuntimeError("current schema line was not exported")
|
|
166
|
+
(catalog_directory / "index.json").write_bytes(current_catalog)
|
|
167
|
+
try:
|
|
168
|
+
publish_path_no_replace(staging, destination_path)
|
|
169
|
+
except FileExistsError as error:
|
|
170
|
+
raise SchemaExportError(
|
|
171
|
+
f"schema export destination appeared while exporting: "
|
|
172
|
+
f"{destination_path}"
|
|
173
|
+
) from error
|
|
174
|
+
except BaseException:
|
|
175
|
+
shutil.rmtree(staging, ignore_errors=True)
|
|
176
|
+
raise
|
|
177
|
+
|
|
178
|
+
return SchemaExport(destination=destination_path, schemas=tuple(published))
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
3
|
+
"$id": "https://greaterexpanse.com/itself/schemas/v0alpha/evidence-bundle.schema.json",
|
|
4
|
+
"title": "Itself Evidence Bundle v0alpha",
|
|
5
|
+
"description": "A closed, digest-inventoried package containing an evidence ledger, its reasoning receipt, and referenced artifacts.",
|
|
6
|
+
"type": "object",
|
|
7
|
+
"additionalProperties": false,
|
|
8
|
+
"required": [
|
|
9
|
+
"bundle_version",
|
|
10
|
+
"bundle_id",
|
|
11
|
+
"title",
|
|
12
|
+
"created_at",
|
|
13
|
+
"closed_artifacts",
|
|
14
|
+
"files",
|
|
15
|
+
"limitations"
|
|
16
|
+
],
|
|
17
|
+
"properties": {
|
|
18
|
+
"bundle_version": {
|
|
19
|
+
"const": "0.1.0-alpha.1"
|
|
20
|
+
},
|
|
21
|
+
"bundle_id": {
|
|
22
|
+
"type": "string",
|
|
23
|
+
"pattern": "^urn:sha256:[0-9a-f]{64}$"
|
|
24
|
+
},
|
|
25
|
+
"title": {
|
|
26
|
+
"type": "string",
|
|
27
|
+
"minLength": 1
|
|
28
|
+
},
|
|
29
|
+
"created_at": {
|
|
30
|
+
"type": "string",
|
|
31
|
+
"format": "date-time"
|
|
32
|
+
},
|
|
33
|
+
"closed_artifacts": {
|
|
34
|
+
"const": true
|
|
35
|
+
},
|
|
36
|
+
"files": {
|
|
37
|
+
"type": "array",
|
|
38
|
+
"minItems": 3,
|
|
39
|
+
"items": {
|
|
40
|
+
"$ref": "#/$defs/file"
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
"limitations": {
|
|
44
|
+
"type": "array",
|
|
45
|
+
"minItems": 1,
|
|
46
|
+
"uniqueItems": true,
|
|
47
|
+
"items": {
|
|
48
|
+
"type": "string",
|
|
49
|
+
"minLength": 1
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
"$defs": {
|
|
54
|
+
"sha256": {
|
|
55
|
+
"type": "string",
|
|
56
|
+
"pattern": "^[0-9a-f]{64}$"
|
|
57
|
+
},
|
|
58
|
+
"identifier": {
|
|
59
|
+
"type": "string",
|
|
60
|
+
"minLength": 1,
|
|
61
|
+
"maxLength": 256,
|
|
62
|
+
"pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]*$"
|
|
63
|
+
},
|
|
64
|
+
"file": {
|
|
65
|
+
"type": "object",
|
|
66
|
+
"additionalProperties": false,
|
|
67
|
+
"required": [
|
|
68
|
+
"path",
|
|
69
|
+
"role",
|
|
70
|
+
"media_type",
|
|
71
|
+
"size_bytes",
|
|
72
|
+
"digest"
|
|
73
|
+
],
|
|
74
|
+
"properties": {
|
|
75
|
+
"path": {
|
|
76
|
+
"type": "string",
|
|
77
|
+
"minLength": 1
|
|
78
|
+
},
|
|
79
|
+
"role": {
|
|
80
|
+
"enum": [
|
|
81
|
+
"input",
|
|
82
|
+
"artifact",
|
|
83
|
+
"ledger",
|
|
84
|
+
"reasoning_receipt",
|
|
85
|
+
"supplemental"
|
|
86
|
+
]
|
|
87
|
+
},
|
|
88
|
+
"record_ref": {
|
|
89
|
+
"$ref": "#/$defs/identifier"
|
|
90
|
+
},
|
|
91
|
+
"media_type": {
|
|
92
|
+
"type": "string",
|
|
93
|
+
"minLength": 1
|
|
94
|
+
},
|
|
95
|
+
"size_bytes": {
|
|
96
|
+
"type": "integer",
|
|
97
|
+
"minimum": 0
|
|
98
|
+
},
|
|
99
|
+
"digest": {
|
|
100
|
+
"type": "object",
|
|
101
|
+
"additionalProperties": false,
|
|
102
|
+
"required": [
|
|
103
|
+
"algorithm",
|
|
104
|
+
"value"
|
|
105
|
+
],
|
|
106
|
+
"properties": {
|
|
107
|
+
"algorithm": {
|
|
108
|
+
"const": "sha256"
|
|
109
|
+
},
|
|
110
|
+
"value": {
|
|
111
|
+
"$ref": "#/$defs/sha256"
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
"allOf": [
|
|
117
|
+
{
|
|
118
|
+
"if": {
|
|
119
|
+
"properties": {
|
|
120
|
+
"role": {
|
|
121
|
+
"const": "artifact"
|
|
122
|
+
}
|
|
123
|
+
},
|
|
124
|
+
"required": [
|
|
125
|
+
"role"
|
|
126
|
+
]
|
|
127
|
+
},
|
|
128
|
+
"then": {
|
|
129
|
+
"required": [
|
|
130
|
+
"record_ref"
|
|
131
|
+
]
|
|
132
|
+
},
|
|
133
|
+
"else": {
|
|
134
|
+
"not": {
|
|
135
|
+
"required": [
|
|
136
|
+
"record_ref"
|
|
137
|
+
]
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
]
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|