"""Carry trusted C-generated series JSON without a Python float-array round trip. Only native output plus its byte index may construct this transport object. Public JSON still has the ordinary object/array schema; synchronous callers materialize it explicitly. Bytes own their lifetime independently of the worker directory. """ from dataclasses import dataclass import json from pathlib import Path @dataclass(frozen=True) class NativeSeriesJson: data: bytes sample_count: int def materialize(self) -> dict[str, list[float]]: return json.loads(self.data) def read_indexed_result(output: Path, index_path: Path) -> dict: index = json.loads(index_path.read_bytes()) if not isinstance(index, dict) or type(index.get('version')) is not int or index['version'] != 1: raise ValueError('Unsupported native result index.') names = ('seriesStart', 'seriesEnd', 'resultBytes', 'sampleCount') if any(type(index.get(key)) is not int for key in names): raise ValueError('Invalid native result index integers.') start, end, length, count = (index[key] for key in names) if not (0 < start < end < length and count >= 0): raise ValueError('Invalid native result index bounds.') if output.stat().st_size != length: raise ValueError('Incomplete native result file.') raw = output.read_bytes() if (len(raw) != length or not raw[:start].endswith(b'"series":') or raw[start:start+1] != b'{' or raw[end-1:end] != b'}' or not raw[end:].startswith(b',"final":')): raise ValueError('Native result index does not match the output layout.') # The C writer supplies exact boundaries. No search through strings or numeric # arrays, and no large json.loads call: only diagnostics/final values are read. metadata = json.loads(raw[:start] + b'{}' + raw[end:]) if not isinstance(metadata, dict) or metadata.get('series') != {}: raise ValueError('Invalid native result metadata.') metadata['series'] = NativeSeriesJson(raw[start:end], count) return metadata def serialize_result_parts(payload: dict) -> tuple[bytes, ...]: """Return one JSON object in three byte segments, without copying its series. The only raw position is payload.result.series, produced by our C writer; all model-provided labels, messages and keys use the standard JSON encoder. """ result = payload.get('result') series = result.get('series') if isinstance(result, dict) else None if not isinstance(series, NativeSeriesJson): return (json.dumps(payload, ensure_ascii=False, separators=(',', ':')).encode('utf-8'),) metadata = {key: value for key, value in result.items() if key != 'series'} outer = {key: value for key, value in payload.items() if key != 'result'} encoded = json.dumps(metadata, ensure_ascii=False, separators=(',', ':')).encode('utf-8') remainder = json.dumps(outer, ensure_ascii=False, separators=(',', ':')).encode('utf-8') prefix = b'{"result":' + encoded[:-1] + (b',' if metadata else b'') + b'"series":' suffix = b'}' + (b',' + remainder[1:] if outer else b'}') return prefix, series.data, suffix