"""Offline common-initial-worker/1.0 adapter; development defaults unfrozen.""" import json from pathlib import Path import b1_contract observation = b1_contract.observation CONTRACT = "common-initial-worker/1.0" TEMPLATE_PATH = Path(__file__).resolve().parent.parent/"baseline-prompts"/"common-initial-worker.txt" TEMPLATE_SHA256 = "dd21eaf83a75524982f7e2ebd18fea3b8ed3dd26c346f430130e65313ce94e0a" def initial_attempts(run, slots): """Explicitly partition all adaptation worker attempts, with no stage inference. The shared B1 partition convention (one initial root and its retries; stable retry designation) is compatible with the common initial worker interface. Its `continuation` label only means outside the initial-input audit scope; this function does not assert that those requests used the B1 template. """ other = b1_contract.continuation_attempts(run, slots) return {a["attempt_id"]: a for a in run["attempts"] if a["phase"] == "adaptation" and a["role"] == "worker" and a["attempt_id"] not in other} def validate_input(content, bounds, task_id, public_packet_sha256): observation.require(len(content) <= bounds["max_input_bytes"], "initial input byte bound exceeded") template = TEMPLATE_PATH.read_bytes() observation.require(observation.sha(template) == TEMPLATE_SHA256, "initial adapter template hash mismatch") rendered = content.decode("utf-8") observation.require(observation.MARKER.search(rendered) is None, "initial input has unresolved placeholders") wrapper = template.decode("utf-8") observation.require(wrapper.count("{{TASK_PACKET_JSON}}") == 1, "unsupported initial template placeholders") prefix, suffix = wrapper.split("{{TASK_PACKET_JSON}}") observation.require(rendered.startswith(prefix), "initial input does not have the unchanged literal wrapper") start, cursor = len(prefix), len(prefix) while cursor < len(rendered) and rendered[cursor] in " \t\r\n": cursor += 1 _, end = json.JSONDecoder().raw_decode(rendered, cursor) observation.require(rendered[end:] == suffix, "initial input does not have the unchanged literal wrapper") packet = observation.parse_json(rendered[start:end].encode("utf-8"), "initial public packet") observation.text(packet, "initial public packet JSON string") observation.require(observation.sha(packet.encode("utf-8")) == public_packet_sha256, "initial immutable public packet hash mismatch") observation.task_identifiers(packet, task_id) return packet