#!/usr/bin/env python3 """Offline structural validation and staging, never worker delivery or live-memory update.""" import argparse import json from pathlib import Path import re import sys import assemble_observation as observation NOTICE = "Development planner-response tooling; structural validity is not semantic support, permission, or tokenizer verification." BLOCKS = (("IMMUTABLE_TASK_PACKET_JSON", "TASK_PACKET_JSON"), ("CURRENT_ARTIFACT_JSON", "CURRENT_ARTIFACT_JSON"), ("EARLIER_ARTIFACTS_JSON", "EARLIER_ARTIFACTS_JSON"), ("PREVIOUS_WORKER_INSTRUCTIONS_JSON", "PREVIOUS_INSTRUCTIONS_JSON"), ("PERSISTENT_MEMORY_JSON", "PERSISTENT_MEMORY_JSON")) FIELDS = ("observed_gap", "evidence", "uncertainty", "next_prompt", "memory") BOUNDS = ("max_input_bytes", "max_response_bytes", "max_next_prompt_bytes", "max_memory_bytes", "max_memory_entries", "max_evidence_references", "max_next_prompt_tokens", "max_memory_tokens") def read_bound(descriptor, parent, limit, label): observation.keys(descriptor, ("path", "sha256"), label) path = (parent/observation.text(descriptor["path"], label+" path")).resolve() expected = descriptor["sha256"] observation.require(isinstance(expected, str) and re.fullmatch(r"[0-9a-f]{64}", expected) is not None, "invalid "+label+" SHA-256") observation.require(path.is_file(), "missing/non-file "+label) with path.open("rb") as handle: content = handle.read(limit+1) observation.require(len(content) <= limit, label+" byte bound exceeded") observation.require(observation.sha(content) == expected, label+" hash mismatch") content.decode("utf-8") return path, content def input_context(content, bounds): """Recover canonical values and hash-check the unchanged template skeleton.""" rendered = content.decode("utf-8") observation.require(observation.MARKER.search(rendered) is None, "assembled input has unresolved placeholders") values, substitutions = {}, [] for label, placeholder in BLOCKS: matches = list(re.finditer(r"(?m)^"+re.escape(label)+r"\n([^\r\n]*)\n", rendered)) observation.require(len(matches) == 1, "missing/duplicate assembled input block: "+label) match = matches[0] raw = match.group(1) value = observation.parse_json(raw.encode("utf-8"), label) observation.require(observation.canonical(value) == raw, "assembled block is not the canonical assembler representation") values[placeholder] = value substitutions.append((match.start(1), match.end(1), "{{"+placeholder+"}}")) skeleton = rendered for start, end, placeholder in sorted(substitutions, reverse=True): skeleton = skeleton[:start]+placeholder+skeleton[end:] observation.require(observation.sha(skeleton.encode("utf-8")) == observation.TEMPLATE_SHA256, "assembled input does not have the unchanged matched treatment wrapper") packet = values["TASK_PACKET_JSON"] observation.text(packet, "public task packet") heading = re.match(r"\A\ufeff?#\s+([^:\s]+)", packet) observation.require(heading is not None, "public packet requires a task-ID heading") task_id = heading.group(1) requirement_ids, source_ids = observation.task_identifiers(packet, task_id) observation.require(task_id not in requirement_ids | source_ids, "ambiguous public task identity") reserved = {task_id, *requirement_ids, *source_ids} artifacts = set() def identity(value, label): observation.text(value, label) observation.require(value not in reserved, "duplicate/ambiguous supplied identity") reserved.add(value) current = values["CURRENT_ARTIFACT_JSON"] earlier = values["EARLIER_ARTIFACTS_JSON"] previous = values["PREVIOUS_INSTRUCTIONS_JSON"] observation.require(isinstance(earlier, list) and len(earlier) <= 2, "invalid supplied artifact history") observation.require(isinstance(previous, list) and len(previous) <= 12, "invalid supplied instruction history") for artifact in [current, *earlier]: observation.keys(artifact, ("artifact_id", "text"), "supplied artifact") identity(artifact["artifact_id"], "artifact_id") observation.require(isinstance(artifact["text"], str), "artifact text must be a string") artifact["text"].encode("utf-8") artifacts.add(artifact["artifact_id"]) for instruction in previous: observation.keys(instruction, ("instruction_id", "text"), "supplied instruction") identity(instruction["instruction_id"], "instruction_id") observation.text(instruction["text"], "submitted instruction text") catalogs = dict(artifact_id=artifacts, requirement_id=requirement_ids, source_id=source_ids) # Existing live IDs may be reused in a full replacement proposal. They are # not added to the reserved artifact/task/instruction namespace for responses. observation.validate_typed_memory(values["PERSISTENT_MEMORY_JSON"], catalogs, set(reserved), bounds["max_memory_entries"]) return catalogs, reserved def check_attestation(attestation, value_bytes, cap, label): observation.keys(attestation, ("tokenizer", "sha256", "token_count"), label+" token attestation") observation.text(attestation["tokenizer"], label+" declared tokenizer") count = observation.integer(attestation["token_count"], 1, label+" declared token count") observation.require(count <= cap, label+" declared token cap exceeded") observation.require(attestation["sha256"] == observation.sha(value_bytes), label+" token attestation hash mismatch") def validate_response(manifest_path, staging_dir, validation_dir): manifest_path = Path(manifest_path).resolve() staging, audit = observation.new_output(staging_dir), observation.new_output(validation_dir) observation.require(staging != audit and staging not in audit.parents and audit not in staging.parents, "staging and coordinator validation destinations must be disjoint") manifest_bytes = manifest_path.read_bytes() manifest = observation.parse_json(manifest_bytes, "validation manifest") observation.keys(manifest, ("validation_manifest_version", "validation_id", "assembled_input", "planner_response", "bounds", "token_attestations"), "validation manifest") observation.require(manifest["validation_manifest_version"] == "1.0", "unsupported validation manifest version") observation.text(manifest["validation_id"], "validation_id") bounds = manifest["bounds"] observation.keys(bounds, BOUNDS, "validation bounds") for key, value in bounds.items(): observation.integer(value, 0 if key in ("max_memory_entries", "max_evidence_references") else 1, key) observation.require(bounds["max_next_prompt_tokens"] <= 800 and bounds["max_memory_tokens"] <= 1200, "token caps cannot exceed the matched 800/1,200 contract") input_path, input_bytes = read_bound(manifest["assembled_input"], manifest_path.parent, bounds["max_input_bytes"], "assembled input") catalogs, reserved = input_context(input_bytes, bounds) response_path, raw = read_bound(manifest["planner_response"], manifest_path.parent, bounds["max_response_bytes"], "planner response") response = observation.parse_json(raw, "planner response") observation.keys(response, FIELDS, "planner response") for field in ("observed_gap", "uncertainty"): observation.require(isinstance(response[field], str), field+" must be a string") response[field].encode("utf-8") directive = observation.text(response["next_prompt"], "next_prompt").encode("utf-8") observation.require(len(directive) <= bounds["max_next_prompt_bytes"], "next_prompt byte bound exceeded") observation.validate_evidence(response["evidence"], catalogs, allow_empty=True, max_references=bounds["max_evidence_references"]) observation.validate_typed_memory(response["memory"], catalogs, set(reserved), bounds["max_memory_entries"], max_references=bounds["max_evidence_references"]) memory = observation.canonical(response["memory"]).encode("utf-8") observation.require(len(memory) <= bounds["max_memory_bytes"], "memory byte bound exceeded") attestations = manifest["token_attestations"] observation.keys(attestations, ("next_prompt", "memory"), "token attestations") check_attestation(attestations["next_prompt"], directive, bounds["max_next_prompt_tokens"], "next_prompt") check_attestation(attestations["memory"], memory, bounds["max_memory_tokens"], "memory") record = dict(validator_version="1.0", notice=NOTICE, defaults_unfrozen=True, validation_id=manifest["validation_id"], validation_manifest_sha256=observation.sha(manifest_bytes), assembled_input_path=str(input_path), assembled_input_sha256=observation.sha(input_bytes), raw_response_path=str(response_path), raw_response_sha256=observation.sha(raw), bounds=bounds, token_attestations=attestations, tokenizer_counts_independently_verified=False, semantic_support_verified=False, restricted_requests_verified=False, structurally_valid=True, worker_directive=dict(path=str(staging/"next-prompt.txt"), sha256=observation.sha(directive), bytes=len(directive)), proposed_memory=dict(path=str(staging/"proposed-memory.json"), sha256=observation.sha(memory), bytes=len(memory)), next_assembly_memory_descriptor=dict(path=str(staging/"proposed-memory.json"), sha256=observation.sha(memory), token_attestation=dict(tokenizer=attestations["memory"]["tokenizer"], serialized_sha256=observation.sha(memory), token_count=attestations["memory"]["token_count"])), directive_delivered=False, live_memory_replaced=False, staging_complete=True) staging.mkdir() audit.mkdir() with (staging/"next-prompt.txt").open("xb") as handle: handle.write(directive) # Exact decoded bytes; no strip or appended newline. with (staging/"proposed-memory.json").open("xb") as handle: handle.write(memory) # Full replacement proposal, not a merge or live update. with (audit/"validation-record.json").open("xb") as handle: handle.write((json.dumps(record, indent=2, ensure_ascii=False, allow_nan=False)+"\n").encode("utf-8")) return dict(validator_version="1.0", notice=NOTICE, staging_dir=str(staging), validation_record=str(audit/"validation-record.json"), assembled_input_sha256=observation.sha(input_bytes), directive_delivered=False, live_memory_replaced=False) def main(argv=None): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--manifest", type=Path, required=True) parser.add_argument("--staging-dir", type=Path, required=True) parser.add_argument("--validation-dir", type=Path, required=True) args = parser.parse_args(argv) try: result = validate_response(args.manifest, args.staging_dir, args.validation_dir) except (observation.AssemblyError, OSError, KeyError, TypeError, ValueError) as exc: print(json.dumps(dict(error=str(exc), notice=NOTICE)), file=sys.stderr) return 2 print(json.dumps(result, indent=2)) return 0 if __name__ == "__main__": sys.exit(main())