File size: 13,002 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 | """Literal definition -> TOMAGI EMIT graph -> generic byte materialization."""
from __future__ import annotations
import base64
import json
import math
from pathlib import Path
from typing import Any, Mapping, Sequence
from tomagi.core import Cell, Opcode, Program, State, FLAG_EMIT_HALT, pack_key_contiguous, run
from tomagi.format import dump, load
from .canonical import attach_hash, canonical_bytes, digest_bytes, verify_hash
from .records import make_record, topological_record_order, validate_record
from .seed import verify_seed_bytes
ARTIFACT_SOURCE_SCHEMA = "TOM-LITERAL-ARTIFACT-SOURCE-0.1"
ARTIFACT_COMPILE_REPORT_SCHEMA = "TOM-LITERAL-ARTIFACT-COMPILE-REPORT-0.1"
PROGRAM_FLAG_LITERAL_ARTIFACT = 1 << 8
PROGRAM_FLAG_EMIT_BYTES = 1 << 9
FLAG_EMIT_COUNT_SHIFT = 8
FLAG_EMIT_COUNT_MASK = 0x7 << FLAG_EMIT_COUNT_SHIFT
FLAG_EMIT_BIG_ENDIAN = 1 << 11
MAX_ARTIFACT_BYTES = 1_048_576
def _decode_literal(parameters: Mapping[str, Any]) -> bytes:
encoding = parameters.get("encoding")
data = parameters.get("data")
if not isinstance(encoding, str) or not isinstance(data, str):
raise ValueError("literal.bytes requires string encoding and data")
if encoding in {"utf8", "utf-8"}:
return data.encode("utf-8")
if encoding == "ascii":
return data.encode("ascii")
if encoding == "hex":
return bytes.fromhex(data)
if encoding == "base64":
return base64.b64decode(data, validate=True)
raise ValueError(f"unsupported literal bytes encoding: {encoding}")
def make_literal_artifact_source(
artifact_id: str,
data: bytes,
*,
media_type: str,
seed_bytes: bytes,
provenance: Mapping[str, Any] | None = None,
) -> dict[str, Any]:
identity = verify_seed_bytes(seed_bytes)
seed_def = make_record(
"definition",
"definition:canonical-seed",
{
"kind": "canonical_seed",
"domain": "none",
"codomain": "bytes",
"operation": "seed.bytes",
"phase": "parse",
"order": 0,
"parameters": {},
},
provenance={"source": "TOM_seed_genome_2026-09-01.txt"},
)
literal_def = make_record(
"definition",
f"definition:{artifact_id}:bytes",
{
"kind": "literal_artifact_bytes",
"domain": "canonical_seed",
"codomain": "bytes",
"operation": "literal.bytes",
"phase": "construct",
"order": 0,
"parameters": {
"encoding": "base64",
"data": base64.b64encode(data).decode("ascii"),
"media_type": media_type,
},
},
dependencies=[seed_def["id"]],
provenance=dict(provenance or {}),
)
emit_def = make_record(
"definition",
f"definition:{artifact_id}:emit",
{
"kind": "generic_byte_emission",
"domain": "bytes",
"codomain": "cell_graph",
"operation": "emit.bytes",
"phase": "transition",
"order": 0,
"parameters": {
"chunk_bytes": 4,
"byte_order": "little",
"halt_last": True,
"rho_start": 1,
},
},
dependencies=[literal_def["id"]],
provenance={"profile": "TOM-EMIT-BYTES-0.1"},
)
return attach_hash({
"schema": ARTIFACT_SOURCE_SCHEMA,
"version": "0.1.0",
"seed_sha256": "sha256:" + identity.sha256,
"artifact_id": artifact_id,
"media_type": media_type,
"root_definition": emit_def["id"],
"max_output_bytes": MAX_ARTIFACT_BYTES,
"definitions": [seed_def, literal_def, emit_def],
})
def _evaluate_source(source: Mapping[str, Any], seed_bytes: bytes) -> tuple[bytes, list[str]]:
identity = verify_seed_bytes(seed_bytes)
if source.get("schema") != ARTIFACT_SOURCE_SCHEMA or not verify_hash(source):
raise ValueError("literal artifact source hash/schema is invalid")
if source.get("seed_sha256") != "sha256:" + identity.sha256:
raise ValueError("literal artifact source is not bound to the canonical seed")
definitions = source.get("definitions")
if not isinstance(definitions, list) or not definitions:
raise ValueError("literal artifact source requires definitions")
order = topological_record_order(definitions)
by_id = {record["id"]: record for record in definitions}
values: dict[str, bytes] = {}
for ident in order:
record = by_id[ident]
validate_record(record)
if record["record_type"] != "definition":
raise TypeError("artifact definitions must use record_type definition")
payload = record["payload"]
operation = payload["operation"]
dependencies = record["dependencies"]
if operation == "seed.bytes":
if dependencies:
raise ValueError("seed.bytes must not have dependencies")
value = seed_bytes
elif operation == "literal.bytes":
value = _decode_literal(payload.get("parameters", {}))
elif operation == "concat.bytes":
if not dependencies:
raise ValueError("concat.bytes requires dependencies")
value = b"".join(values[dep] for dep in dependencies)
elif operation == "emit.bytes":
if len(dependencies) != 1:
raise ValueError("emit.bytes requires exactly one bytes dependency")
value = values[dependencies[0]]
else:
raise ValueError(f"unsupported artifact definition operation: {operation}")
if len(value) > int(source.get("max_output_bytes", MAX_ARTIFACT_BYTES)):
raise ValueError("artifact output byte budget exceeded")
values[ident] = value
root = source.get("root_definition")
if not isinstance(root, str) or root not in values:
raise ValueError("artifact root_definition does not resolve")
if by_id[root]["payload"]["operation"] != "emit.bytes":
raise ValueError("artifact root_definition must execute emit.bytes")
return values[root], order
def compile_literal_artifact(
source: Mapping[str, Any],
seed_bytes: bytes,
) -> tuple[Program, dict[str, Any]]:
data, order = _evaluate_source(source, seed_bytes)
if not data:
raise ValueError("empty literal artifacts are not supported in 0.1")
root = next(record for record in source["definitions"] if record["id"] == source["root_definition"])
parameters = root["payload"].get("parameters", {})
chunk_bytes = int(parameters.get("chunk_bytes", 4))
if not 1 <= chunk_bytes <= 4:
raise ValueError("emit.bytes chunk_bytes must be in 1..4")
byte_order = parameters.get("byte_order", "little")
if byte_order not in {"little", "big"}:
raise ValueError("emit.bytes byte_order must be little or big")
rho_start = int(parameters.get("rho_start", 1))
cell_count = math.ceil(len(data) / chunk_bytes)
if rho_start < 0 or rho_start + cell_count > (1 << 20):
raise ValueError("artifact EMIT keys exceed the 20-bit rho field")
cells: list[Cell] = []
crosswalk: list[dict[str, Any]] = []
for index in range(cell_count):
chunk = data[index * chunk_bytes:(index + 1) * chunk_bytes]
count = len(chunk)
flags = count << FLAG_EMIT_COUNT_SHIFT
if byte_order == "big":
flags |= FLAG_EMIT_BIG_ENDIAN
payload = int.from_bytes(chunk.rjust(4, b"\0"), "big")
else:
payload = int.from_bytes(chunk.ljust(4, b"\0"), "little")
if parameters.get("halt_last", True) and index == cell_count - 1:
flags |= FLAG_EMIT_HALT
hi, lo = pack_key_contiguous(rho_start + index, 0, 0, 0)
next_index = index + 1 if index + 1 < cell_count else index
cells.append(Cell(
hi,
lo,
int(Opcode.EMIT),
flags,
0,
0,
0,
0,
next_index,
next_index,
payload,
index,
))
crosswalk.append({
"cell_index": index,
"definition_id": root["id"],
"byte_offset": index * chunk_bytes,
"byte_count": count,
"payload": payload,
"flags": flags,
"key": f"0x{((hi << 32) | lo):016x}",
})
program = Program(
cells=cells,
entry=0,
seed=int(source["seed_sha256"][7:15], 16),
default_ticks=cell_count,
initial_state=State(),
flags=PROGRAM_FLAG_LITERAL_ARTIFACT | PROGRAM_FLAG_EMIT_BYTES,
)
report = attach_hash({
"schema": ARTIFACT_COMPILE_REPORT_SCHEMA,
"artifact_id": source["artifact_id"],
"source_hash": source["content_hash"],
"seed_sha256": source["seed_sha256"],
"definition_order": order,
"definition_hashes": {record["id"]: record["content_hash"] for record in source["definitions"]},
"artifact_bytes": len(data),
"artifact_sha256": digest_bytes(data),
"cell_count": cell_count,
"crosswalk": crosswalk,
})
return program, report
def compile_literal_artifact_file(
source_path: str | Path,
seed_path: str | Path,
program_path: str | Path,
) -> dict[str, Any]:
source_file = Path(source_path)
source = json.loads(source_file.read_text(encoding="utf-8"))
program, report = compile_literal_artifact(source, Path(seed_path).read_bytes())
destination = Path(program_path)
destination.parent.mkdir(parents=True, exist_ok=True)
dump(program, destination)
report = dict(report)
report["program_bytes"] = destination.stat().st_size
report["program_sha256"] = digest_bytes(destination.read_bytes())
report = attach_hash({key: value for key, value in report.items() if key != "content_hash"})
destination.with_suffix(destination.suffix + ".compile.json").write_bytes(canonical_bytes(report) + b"\n")
return report
def decode_emit_cell(cell: Cell) -> bytes:
if cell.opcode != int(Opcode.EMIT):
raise ValueError("cell is not EMIT")
count = (cell.flags & FLAG_EMIT_COUNT_MASK) >> FLAG_EMIT_COUNT_SHIFT
if not 1 <= count <= 4:
raise ValueError(f"EMIT byte count must be in 1..4, got {count}")
if cell.flags & FLAG_EMIT_BIG_ENDIAN:
return cell.payload.to_bytes(4, "big")[-count:]
return cell.payload.to_bytes(4, "little")[:count]
def materialize_trace(program: Program, trace: Sequence[Mapping[str, Any]]) -> tuple[bytes, list[dict[str, Any]]]:
if not (program.flags & PROGRAM_FLAG_LITERAL_ARTIFACT) or not (program.flags & PROGRAM_FLAG_EMIT_BYTES):
raise ValueError("program does not declare the literal emitted-byte profile")
output = bytearray()
records: list[dict[str, Any]] = []
for trace_record in trace:
cell_index = int(trace_record["cell_before"])
if not 0 <= cell_index < len(program.cells):
raise ValueError("trace cell index is outside program")
cell = program.cells[cell_index]
if cell.opcode != int(Opcode.EMIT):
continue
chunk = decode_emit_cell(cell)
output.extend(chunk)
records.append({
"sequence": len(records),
"step": int(trace_record["step"]),
"cell_index": cell_index,
"byte_count": len(chunk),
"hex": chunk.hex(),
"flags": cell.flags,
"payload": cell.payload,
"lineage": int(trace_record["lineage"]),
})
if not records:
raise ValueError("trace contains no emitted-byte records")
return bytes(output), records
def run_and_materialize(program: Program) -> tuple[bytes, dict[str, Any], list[dict[str, Any]], list[dict[str, Any]]]:
state, trace = run(program, ticks=program.default_ticks, trace=True)
data, records = materialize_trace(program, trace)
return data, {name: getattr(state, name) for name in state.__dataclass_fields__}, trace, records
def materialize_literal_artifact_file(
program_path: str | Path,
destination_path: str | Path,
*,
trace_path: str | Path | None = None,
records_path: str | Path | None = None,
) -> dict[str, Any]:
program = load(program_path)
data, state, trace, records = run_and_materialize(program)
destination = Path(destination_path)
destination.parent.mkdir(parents=True, exist_ok=True)
destination.write_bytes(data)
if trace_path is not None:
Path(trace_path).write_bytes(canonical_bytes({"state": state, "trace": trace}) + b"\n")
if records_path is not None:
Path(records_path).write_bytes(canonical_bytes(records) + b"\n")
return {
"artifact_bytes": len(data),
"artifact_sha256": digest_bytes(data),
"emit_records": len(records),
"final_state": state,
}
|