|
| 1 | +"""Deterministic umbrella evidence envelope for harness runs.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import hashlib |
| 6 | +import json |
| 7 | +from datetime import UTC, datetime |
| 8 | + |
| 9 | + |
| 10 | +def _digest(value: object) -> str: |
| 11 | + encoded = json.dumps(value, sort_keys=True, separators=(",", ":")).encode() |
| 12 | + return hashlib.sha256(encoded).hexdigest() |
| 13 | + |
| 14 | + |
| 15 | +def add_envelope(payload: dict, *, repo: str, profile: str, plan_hash: str) -> dict: |
| 16 | + identity = {"repo": repo, "commit": "0000000", "harness": "helios-harness", "model": "unknown", "task_id": f"plan:{plan_hash}", "hardware": "unknown"} |
| 17 | + digest = _digest(identity) |
| 18 | + run_id = f"run_{digest}" |
| 19 | + session_id = f"ses_{_digest({'run_id': run_id, 'session': 'default'})}" |
| 20 | + attempt_id = f"att_{_digest({'run_id': run_id, 'attempt': 0})}" |
| 21 | + causality = {"tenant_id": "phenotype", "session_id": session_id, "run_id": run_id, "attempt_id": attempt_id} |
| 22 | + now = datetime.now(UTC).isoformat() |
| 23 | + checkpoint_id = f"cp_{_digest({'run_id': run_id, 'checkpoint': 0})[:16]}" |
| 24 | + events = [] |
| 25 | + for seq, event_type in enumerate(("run_started", "checkpoint", "compaction", "run_finished")): |
| 26 | + event = {"event_id": f"evt_{_digest({'run_id': run_id, 'seq': seq, 'type': event_type})}", "seq": seq, "ts": now, "type": event_type, "payload_sha256": _digest({"type": event_type, "seq": seq}), "causality": causality} |
| 27 | + if event_type in ("checkpoint", "compaction"): |
| 28 | + event["checkpoint_id"] = checkpoint_id |
| 29 | + if event_type == "compaction": |
| 30 | + event["details"] = {"tokens_before": 0, "tokens_after": 0, "retained_event_ids": [], "dropped_event_ids": []} |
| 31 | + events.append(event) |
| 32 | + passed = payload.get("result_code") == "PASS" |
| 33 | + legacy_digest = _digest(payload) |
| 34 | + return { |
| 35 | + "schema_version": "1.0.0", |
| 36 | + "tenant_id": "phenotype", "session_id": session_id, "run_id": run_id, "attempt_id": attempt_id, |
| 37 | + "deterministic_identity": {"algorithm": "sha256(canonical-json(inputs))", "canonical_json_sha256": digest, "inputs": identity}, |
| 38 | + "subject": {"repo": repo, "commit": "unknown", "harness": "helios-harness", "runtime": "python", "model": "unknown", "hardware": "unknown"}, |
| 39 | + "lease": {"lease_id": f"lease_{attempt_id[4:20]}", "owner": "helios-harness", "ttl_seconds": 120, "heartbeat_interval_seconds": 20}, |
| 40 | + "task_manifest": {"task_id": f"plan:{plan_hash}", "input_sha256": plan_hash, "timeout_seconds": 1, "assertions": [{"id": "plan_discovered", "kind": "command_plan", "expected": True}], "judge": {"name": "helios-harness", "version": "0.1.0"}}, |
| 41 | + "events": events, |
| 42 | + "result": {"status": "passed" if passed else "failed", "outcome_sha256": legacy_digest, "replay_hash": _digest(events), "failure_class": "none" if passed else "unknown", "artifacts": [{"kind": "report", "uri": f"urn:helios:legacy-evidence:{legacy_digest}", "sha256": legacy_digest}]}, |
| 43 | + "provenance": {"collector": "helios-harness", "collected_at": now, "source_hashes": {"plan": plan_hash}}, |
| 44 | + "signature": {"algorithm": "placeholder", "key_id": "unconfigured", "signature_b64": ""}, |
| 45 | + } |
0 commit comments