"""QE-BATCH-01/1.0: public standalone extract of the batches check. Generated from qe_workflow.py; data are invented and results are limited to declared identifiers. No network access or third-party dependencies. """ import argparse import hashlib import json from pathlib import Path import sys VERSION = "3.0.0-pilot" MAX_BYTES = 5_000_000 MAX_ROWS = 50_000 def read_json(filename): if Path(filename).stat().st_size > MAX_BYTES: raise ValueError("SIZE: maximum 5 MB") raw = Path(filename).read_bytes() if len(raw) > MAX_BYTES: raise ValueError("SIZE: maximum 5 MB") def pairs(items): obj = {} for key, value in items: if key in obj: raise ValueError("JSON: duplicate key") obj[key] = value return obj def constant(value): raise ValueError("JSON: nonfinite number") obj = json.loads(raw.decode("utf-8-sig"), object_pairs_hook=pairs, parse_constant=constant) return obj, hashlib.sha256(raw).hexdigest() def identity(value): if not isinstance(value, str) or not value.strip() or len(value) > 200: raise ValueError("ID: nonempty string of at most 200 characters required") return value def unique(values): values = [identity(v) for v in values] if len(values) != len(set(values)): raise ValueError("ID: duplicate identity") return values def batch_audit(obj): """Check request partition and response identities, not quality of AI answers.""" if not isinstance(obj, dict) or any(not isinstance(obj.get(k), list) for k in ('targets', 'requests', 'responses')): raise ValueError("SCHEMA: targets, requests and responses must be arrays") if any(not isinstance(r, dict) or not isinstance(r.get('ids'), list) for r in obj['requests'] + obj['responses']): raise ValueError("SCHEMA: each batch requires an ids array") targets = set(unique(obj["targets"])) if not targets or len(targets) > MAX_ROWS: raise ValueError("TARGETS: 1..50000 required") requests, responses = obj["requests"], obj["responses"] unique(r["batch_id"] for r in requests) unique(r["batch_id"] for r in responses) if len(requests) + len(responses) > MAX_ROWS: raise ValueError("ROWS: too many batches") assigned, seen, lookup, issues = {}, {}, {}, [] for req in requests: batch = identity(req["batch_id"]) ids = unique(req["ids"]) lookup[batch] = set(ids) for key in ids: if key not in targets: issues.append({"code": "REQUEST_OUTSIDE_TARGETS", "batch": batch, "id": key}) if key in assigned: issues.append({"code": "DUPLICATE_ASSIGNMENT", "batch": batch, "id": key}) assigned[key] = batch for res in responses: batch = identity(res["batch_id"]) if batch not in lookup: issues.append({"code": "UNKNOWN_BATCH", "batch": batch}) # Duplicates in responses are findings, not silently deduplicated. for value in res["ids"]: key = identity(value) if key not in lookup.get(batch, set()): issues.append({"code": "RESPONSE_OUTSIDE_REQUEST", "batch": batch, "id": key}) if key in seen: issues.append({"code": "DUPLICATE_RESPONSE", "batch": batch, "id": key}) seen[key] = batch for key in sorted(targets - set(assigned)): issues.append({"code": "UNASSIGNED_TARGET", "id": key}) response_lookup = {r["batch_id"]: set(r["ids"]) for r in responses} for batch, ids in lookup.items(): actual = response_lookup.get(batch, set()) for key in sorted(ids - actual): issues.append({"code": "MISSING_RESPONSE_IN_BATCH", "batch": batch, "id": key}) return {"scope": "declared_identity_partition_only", "targets": len(targets), "issues": issues, "not_assessed": ["answer_quality", "model_context", "economic_benefit", "retry_idempotency"]} def main(): parser = argparse.ArgumentParser(description="QE-BATCH-01/1.0: declared identifiers only; invented teaching inputs.") parser.add_argument("input") parser.add_argument("--out", required=True) args = parser.parse_args() try: obj, digest = read_json(args.input) result = batch_audit(obj) envelope = {"version": VERSION, "command": "batches", "input_sha256": [digest], "result": result, "verdict": "ANOMALIES" if result["issues"] else "NO_FINDINGS_IN_SCOPE"} with Path(args.out).open("x", encoding="utf-8") as handle: handle.write(json.dumps(envelope, ensure_ascii=False, allow_nan=False, indent=2) + "\n") return 0 except (OSError, ValueError, KeyError, TypeError, AttributeError) as exc: print("ERROR: " + str(exc), file=sys.stderr) return 2 if __name__ == "__main__": sys.exit(main())