File size: 18,348 Bytes
767a051 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 | """Independent reference oracle for TOM Learner 0.1 promotion authority 0.5.2.
This module is deliberately not the authority. The authoritative semantics are
``examples/learner052/promotion_authority.formal.json`` evaluated inside the
seeded TOMAGI definition graph. The oracle is a separately written ordinary
Python reconstruction used to detect formal-program mistakes.
"""
from __future__ import annotations
import copy
import hashlib
import json
from typing import Any, Mapping, Sequence
class OracleError(ValueError):
pass
def canonical_bytes(value: Any) -> bytes:
return json.dumps(
value,
sort_keys=True,
ensure_ascii=False,
separators=(",", ":"),
allow_nan=False,
).encode("utf-8")
def address(value: Any) -> str:
return "sha256:" + hashlib.sha256(canonical_bytes(value)).hexdigest()
def addressed(body: Mapping[str, Any]) -> dict[str, Any]:
result = copy.deepcopy(dict(body))
if "content_hash" in result:
raise OracleError("addressed body must not already contain content_hash")
result["content_hash"] = address(result)
return result
def verify_addressed(record: Mapping[str, Any], label: str) -> dict[str, Any]:
if not isinstance(record, Mapping):
raise OracleError(f"{label} must be an object")
value = copy.deepcopy(dict(record))
claimed = value.pop("content_hash", None)
if claimed != address(value):
raise OracleError(f"{label} content hash mismatch")
value["content_hash"] = claimed
return value
def _write(namespace: str, record: Mapping[str, Any]) -> dict[str, Any]:
return {"namespace": namespace, "record": copy.deepcopy(dict(record))}
def _unique(values: Sequence[str], label: str) -> list[str]:
result = list(values)
if len(result) != len(set(result)):
raise OracleError(f"{label} must be unique")
return result
def _dataset_map(datasets: Sequence[Mapping[str, Any]]) -> dict[str, dict[str, Any]]:
result: dict[str, dict[str, Any]] = {}
for index, dataset in enumerate(datasets):
checked = verify_addressed(dataset, f"dataset {index}")
ident = checked.get("id")
if not isinstance(ident, str) or not ident:
raise OracleError("dataset id must be a non-empty string")
if ident in result:
raise OracleError(f"duplicate dataset id {ident}")
result[ident] = checked
return result
def build_promotion_result(
promotion_program: Mapping[str, Any],
learner_program: Mapping[str, Any],
learner_execution: Mapping[str, Any],
datasets: Sequence[Mapping[str, Any]],
context: Mapping[str, Any],
corrective_handoff: Mapping[str, Any],
token_registry: Mapping[str, Any],
) -> dict[str, Any]:
promotion_program = verify_addressed(promotion_program, "promotion program")
learner_program = verify_addressed(learner_program, "learner program")
learner_execution = verify_addressed(learner_execution, "learner execution")
learner_value = verify_addressed(learner_execution["value"], "learner value")
context = verify_addressed(context, "promotion context")
corrective_handoff = verify_addressed(corrective_handoff, "corrective handoff")
token_registry = verify_addressed(token_registry, "token registry")
datasets_by_id = _dataset_map(datasets)
if context.get("profile") != "TOM-LEARNER-0.1-PROMOTION-AUTHORITY":
raise OracleError("promotion context profile mismatch")
if context.get("canonical_seed_sha256") != "sha256:d1417a3136772c0cf3eddcd4962ce07d42cbf87616f7b5bae09fc652d9b807b5":
raise OracleError("promotion context seed mismatch")
if corrective_handoff["content_hash"] != context.get("corrective_handoff_content_hash"):
raise OracleError("corrective handoff mismatch")
if token_registry["content_hash"] != context.get("token_registry_content_hash"):
raise OracleError("token registry mismatch")
if learner_program["content_hash"] != context.get("expected_learner_program_content_hash"):
raise OracleError("learner program binding mismatch")
if learner_execution.get("program_hash") != learner_program["content_hash"]:
raise OracleError("learner execution program binding mismatch")
if learner_execution["content_hash"] != context.get("expected_learner_execution_content_hash"):
raise OracleError("learner execution identity mismatch")
if learner_value["content_hash"] != context.get("expected_learner_value_content_hash"):
raise OracleError("learner value identity mismatch")
rows = learner_value.get("results")
if not isinstance(rows, list):
raise OracleError("learner result rows must be an array")
if len(datasets) != context.get("expected_dataset_count") or len(rows) != len(datasets):
raise OracleError("dataset/result count mismatch")
bindings = [{"id": d["id"], "content_hash": d["content_hash"]} for d in datasets]
if bindings != learner_value.get("inputs"):
raise OracleError("literal dataset order differs from learner bindings")
if learner_value.get("accepted_count") != context.get("expected_accepted_count"):
raise OracleError("accepted count mismatch")
if learner_value.get("rejected_count") != context.get("expected_rejected_count"):
raise OracleError("rejected count mismatch")
shared_objects = [
promotion_program,
learner_program,
learner_execution,
learner_value,
context,
corrective_handoff,
token_registry,
]
shared_hashes = _unique([x["content_hash"] for x in shared_objects], "shared authority hashes")
descriptor = addressed({
"schema": "TOMAGI-IMMUTABLE-STORE-DESCRIPTOR-1.0",
"profile": context["profile"],
"seed_sha256": context["canonical_seed_sha256"],
"base_world_hash": context["base_world_hash"],
"base_handoff_hash": context["base_handoff_hash"],
"corrective_handoff_hash": context["corrective_handoff_content_hash"],
"namespaces": copy.deepcopy(context["store_namespaces"]),
"head_namespace": context["head_namespace"],
"record_encoding": context["record_encoding"],
"publication_rule": context["publication_rule"],
})
snapshot = addressed({
"schema": "TOM-LEARNER-PROMOTION-SNAPSHOT-0.5.2",
"profile": context["profile"],
"base_world_hash": context["base_world_hash"],
"base_handoff_hash": context["base_handoff_hash"],
"corrective_handoff_hash": context["corrective_handoff_content_hash"],
"accepted_definitions": {},
"sessions": [],
"accepted_sessions": [],
"rejected_sessions": [],
})
genesis_transaction = addressed({
"schema": "TOM-LEARNER-PROMOTION-GENESIS-TRANSACTION-0.5.2",
"profile": context["profile"],
"sequence": 0,
"parent_commit_hash": None,
"base_world_hash": context["base_world_hash"],
"base_handoff_hash": context["base_handoff_hash"],
"corrective_handoff_hash": context["corrective_handoff_content_hash"],
"seed_sha256": context["canonical_seed_sha256"],
"snapshot_hash": snapshot["content_hash"],
"authority_program_hash": promotion_program["content_hash"],
})
head = addressed({
"schema": "TOM-LEARNER-PROMOTION-COMMIT-0.5.2",
"profile": context["profile"],
"sequence": 0,
"parent_commit_hash": None,
"transaction_hash": genesis_transaction["content_hash"],
"snapshot_hash": snapshot["content_hash"],
"authority_program_hash": promotion_program["content_hash"],
})
genesis_writes = [_write("objects", x) for x in shared_objects] + [
_write("snapshots", snapshot),
_write("transactions", genesis_transaction),
_write("commits", head),
]
genesis_required = shared_hashes + [
snapshot["content_hash"],
genesis_transaction["content_hash"],
head["content_hash"],
]
publications = [addressed({
"schema": "TOMAGI-IMMUTABLE-PUBLICATION-1.0",
"profile": context["profile"],
"sequence": 0,
"expected_head": None,
"replacement_head": head["content_hash"],
"required_hashes": genesis_required,
"writes": genesis_writes,
})]
seen_sessions: set[str] = set()
for row_index, row_value in enumerate(rows):
row = verify_addressed(row_value, f"learner result row {row_index}")
dataset_id = row.get("dataset_id")
if dataset_id not in datasets_by_id:
raise OracleError(f"learner result row names unknown dataset {dataset_id!r}")
if dataset_id in seen_sessions:
raise OracleError(f"duplicate promotion session {dataset_id}")
seen_sessions.add(dataset_id)
dataset = datasets_by_id[dataset_id]
if row.get("dataset_content_hash") != dataset["content_hash"]:
raise OracleError("result row dataset hash differs from literal source")
split_policy = verify_addressed(dataset["split_policy"], "split policy")
hypothesis_family = verify_addressed(dataset["hypothesis_family"], "hypothesis family")
acceptance_policy = verify_addressed(dataset["acceptance_policy"], "acceptance policy")
observations = [
verify_addressed(observation, f"observation {index}")
for index, observation in enumerate(dataset["observations"])
]
accepted = row.get("accepted")
if not isinstance(accepted, bool):
raise OracleError("accepted must be boolean")
relation_value = row.get("relation_definition")
if accepted:
if not isinstance(relation_value, Mapping):
raise OracleError("accepted row has no relation definition")
authority_object = verify_addressed(relation_value, "accepted relation")
rejection_lineage = None
else:
if relation_value is not None:
raise OracleError("rejected row emitted a relation definition")
rejection_lineage = addressed({
"schema": "TOM-LEARNER-REJECTION-LINEAGE-0.5.2",
"profile": context["profile"],
"dataset_id": dataset_id,
"dataset_content_hash": dataset["content_hash"],
"result_row_hash": row["content_hash"],
"reasons": copy.deepcopy(row["acceptance_reasons"]),
"expected_parent_commit_hash": head["content_hash"],
"learner_result_hash": learner_execution["content_hash"],
"authority_program_hash": promotion_program["content_hash"],
})
authority_object = rejection_lineage
decision = addressed({
"schema": "TOM-LEARNER-PROMOTION-DECISION-0.5.2",
"profile": context["profile"],
"dataset_id": dataset_id,
"dataset_content_hash": dataset["content_hash"],
"result_row_hash": row["content_hash"],
"accepted": accepted,
"reasons": copy.deepcopy(row["acceptance_reasons"]),
"published_definition_hash": authority_object["content_hash"] if accepted else None,
"rejection_lineage_hash": None if accepted else authority_object["content_hash"],
"expected_parent_commit_hash": head["content_hash"],
"learner_program_hash": learner_program["content_hash"],
"learner_result_hash": learner_execution["content_hash"],
"authority_program_hash": promotion_program["content_hash"],
})
dataset_objects = [dataset, split_policy, hypothesis_family, acceptance_policy, *observations]
pre_certificate_objects = [*dataset_objects, row, decision, authority_object]
pre_certificate_hashes = _unique(
[*shared_hashes, *[item["content_hash"] for item in pre_certificate_objects]],
"supporting evidence hashes",
)
certificate = addressed({
"schema": "TOM-LEARNER-PROMOTION-CERTIFICATE-0.5.2",
"profile": context["profile"],
"sequence": len(publications),
"dataset_id": dataset_id,
"dataset_content_hash": dataset["content_hash"],
"expected_parent_commit_hash": head["content_hash"],
"result_row_hash": row["content_hash"],
"decision_hash": decision["content_hash"],
"accepted": accepted,
"published_definition_hash": authority_object["content_hash"] if accepted else None,
"rejection_lineage_hash": None if accepted else authority_object["content_hash"],
"supporting_evidence_hashes": pre_certificate_hashes,
"authority_rule": context["authority_statement"],
})
evidence_objects = [*pre_certificate_objects, certificate]
evidence_hashes = [*pre_certificate_hashes, certificate["content_hash"]]
accepted_definitions = copy.deepcopy(snapshot["accepted_definitions"])
accepted_sessions = copy.deepcopy(snapshot["accepted_sessions"])
rejected_sessions = copy.deepcopy(snapshot["rejected_sessions"])
if accepted:
accepted_definitions[dataset_id] = authority_object["content_hash"]
accepted_sessions.append(certificate["content_hash"])
else:
rejected_sessions.append(authority_object["content_hash"])
new_snapshot = addressed({
"schema": "TOM-LEARNER-PROMOTION-SNAPSHOT-0.5.2",
"profile": context["profile"],
"base_world_hash": context["base_world_hash"],
"base_handoff_hash": context["base_handoff_hash"],
"corrective_handoff_hash": context["corrective_handoff_content_hash"],
"accepted_definitions": accepted_definitions,
"sessions": [*snapshot["sessions"], dataset_id],
"accepted_sessions": accepted_sessions,
"rejected_sessions": rejected_sessions,
})
transaction = addressed({
"schema": "TOM-LEARNER-PROMOTION-TRANSACTION-0.5.2",
"profile": context["profile"],
"sequence": len(publications),
"expected_parent_commit_hash": head["content_hash"],
"parent_commit_hash": head["content_hash"],
"parent_snapshot_hash": snapshot["content_hash"],
"base_world_hash": context["base_world_hash"],
"base_handoff_hash": context["base_handoff_hash"],
"corrective_handoff_hash": context["corrective_handoff_content_hash"],
"observation_set_id": dataset_id,
"observation_set_hash": dataset["content_hash"],
"learner_program_hash": learner_program["content_hash"],
"learner_result_hash": learner_execution["content_hash"],
"result_row_hash": row["content_hash"],
"acceptance_decision_hash": decision["content_hash"],
"promotion_certificate_hash": certificate["content_hash"],
"evidence_record_hashes": evidence_hashes,
"accepted": accepted,
"published_definition_hash": authority_object["content_hash"] if accepted else None,
"rejection_lineage_hash": None if accepted else authority_object["content_hash"],
"new_snapshot_hash": new_snapshot["content_hash"],
"authority_program_hash": promotion_program["content_hash"],
"authority_rule": context["authority_statement"],
})
new_head = addressed({
"schema": "TOM-LEARNER-PROMOTION-COMMIT-0.5.2",
"profile": context["profile"],
"sequence": len(publications),
"parent_commit_hash": head["content_hash"],
"transaction_hash": transaction["content_hash"],
"snapshot_hash": new_snapshot["content_hash"],
"authority_program_hash": promotion_program["content_hash"],
})
writes = [_write("objects", item) for item in evidence_objects] + [
_write("snapshots", new_snapshot),
_write("transactions", transaction),
_write("commits", new_head),
]
required_hashes = _unique(
[*evidence_hashes, new_snapshot["content_hash"], transaction["content_hash"], new_head["content_hash"]],
"publication required hashes",
)
publication = addressed({
"schema": "TOMAGI-IMMUTABLE-PUBLICATION-1.0",
"profile": context["profile"],
"sequence": len(publications),
"expected_head": head["content_hash"],
"replacement_head": new_head["content_hash"],
"required_hashes": required_hashes,
"writes": writes,
})
publications.append(publication)
snapshot = new_snapshot
head = new_head
plan = addressed({
"schema": "TOMAGI-IMMUTABLE-PUBLICATION-PLAN-1.0",
"profile": context["profile"],
"store_descriptor": descriptor,
"publications": publications,
"terminal_head": head["content_hash"],
})
return addressed({
"schema": "TOM-LEARNER-PROMOTION-AUTHORITY-RESULT-0.5.2",
"profile": context["profile"],
"authority_program_hash": promotion_program["content_hash"],
"learner_program_hash": learner_program["content_hash"],
"learner_result_hash": learner_execution["content_hash"],
"learner_value_hash": learner_value["content_hash"],
"context_hash": context["content_hash"],
"corrective_handoff_hash": corrective_handoff["content_hash"],
"token_registry_hash": token_registry["content_hash"],
"dataset_count": len(datasets),
"accepted_count": learner_value["accepted_count"],
"rejected_count": learner_value["rejected_count"],
"publication_count": len(publications),
"terminal_head": head["content_hash"],
"terminal_snapshot_hash": snapshot["content_hash"],
"publication_plan": plan,
"claim_boundary": "exact finite affine promotion/evidence transaction authority only; no broader learner family or AGI claim",
})
__all__ = [
"OracleError",
"address",
"addressed",
"build_promotion_result",
"canonical_bytes",
"verify_addressed",
]
|