diff --git a/packages/flow/openadapt_flow/connector/egress.py b/packages/flow/openadapt_flow/connector/egress.py index f4712088f7..37534ac4f6 100644 --- a/packages/flow/openadapt_flow/connector/egress.py +++ b/packages/flow/openadapt_flow/connector/egress.py @@ -9,7 +9,8 @@ * identifiers the control plane issued (run, org, workflow, bundle version, runtime validation, lease job ids); -* SHA-256 digests (bundle archive, read-back spec, effect receipt); +* SHA-256 digests (bundle archive, read-back spec, effect receipt, the stored + report); * closed labels (status, outcome, error code, effect verdict); * structural counts (steps, halts, heals, model calls, cost); * the opaque report key the control plane itself assigned; @@ -189,6 +190,9 @@ def _metrics(value: Any) -> dict[str, Any]: if item is not None: out[key] = _wrap_readback(item) continue + if key == "report_sha256": + out[key] = _hex64(item, "metrics.report_sha256") + continue if key not in _METRIC_KEYS: raise EgressViolation(f"metric {key!r} is not on the allow-list") out[key] = _number(item, key) diff --git a/packages/flow/openadapt_flow/connector/executor.py b/packages/flow/openadapt_flow/connector/executor.py index 81cf516e55..5272b9fc4c 100644 --- a/packages/flow/openadapt_flow/connector/executor.py +++ b/packages/flow/openadapt_flow/connector/executor.py @@ -48,7 +48,11 @@ from openadapt_flow.connector.config import ConnectorSettings from openadapt_flow.connector.protocol import ByocGovernanceError, ByocJob from openadapt_flow.connector.secrets import LocalSecretStore, SecretStoreError -from openadapt_flow.connector.storage import CustomerStorage, extract_bundle_archive +from openadapt_flow.connector.storage import ( + CustomerStorage, + extract_bundle_archive, + report_bytes, +) from openadapt_flow.failure_signals import automation_failure_signal from openadapt_flow.ir import RunReport from openadapt_flow.runner.dispatch_envelope import ( @@ -914,6 +918,10 @@ def _execute_job( metrics = metrics_from_report(report) if readback_evidence is not None: metrics["wrap_readback"] = readback_evidence + if report_ref is not None: + # Only the SHA-256 of the stored report leaves; Cloud seals it with + # the outcome so a receipt can bind this exact report. + metrics["report_sha256"] = hashlib.sha256(report_bytes(report)).hexdigest() return ExecutionResult( status=status, metrics=metrics, diff --git a/packages/flow/openadapt_flow/connector/storage.py b/packages/flow/openadapt_flow/connector/storage.py index 67c066336a..deb410243c 100644 --- a/packages/flow/openadapt_flow/connector/storage.py +++ b/packages/flow/openadapt_flow/connector/storage.py @@ -44,6 +44,11 @@ def write_report(self, ref: Optional[str], report: dict[str, Any]) -> Optional[s ... +def report_bytes(report: dict[str, Any]) -> bytes: + """The exact bytes a report is stored as (its digest may leave; it may not).""" + return json.dumps(report, indent=2).encode("utf-8") + + def _customer_path(root: Path, ref: str, *, kind: str) -> Path: """Resolve one relative customer-storage key without crossing its root.""" if Path(ref).is_absolute() or "\\" in ref: @@ -94,7 +99,7 @@ def write_report(self, ref: Optional[str], report: dict[str, Any]) -> Optional[s return None dest = _customer_path(self.root, ref, kind="report") dest.parent.mkdir(parents=True, exist_ok=True) - dest.write_text(json.dumps(report, indent=2), encoding="utf-8") + dest.write_bytes(report_bytes(report)) try: os.chmod(dest, 0o600) # PHI-bearing — restrict at rest except OSError: # pragma: no cover diff --git a/packages/flow/tests/test_connector_pilot.py b/packages/flow/tests/test_connector_pilot.py index e41d2241d5..a56d6671f5 100644 --- a/packages/flow/tests/test_connector_pilot.py +++ b/packages/flow/tests/test_connector_pilot.py @@ -289,6 +289,19 @@ def runner(argv, run_dir: Path, child_env) -> RunOutcome: # The PHI-bearing report stayed in customer storage. if outcome_kind == "halt_report": assert CANARY_RECORD in json.dumps(storage.written) + # Only the stored report's digest left, so Cloud can bind it. + from openadapt_flow.connector.storage import report_bytes + + callback = next( + json.loads(r.content) + for r in plane.requests + if r.url.path == "/api/internal/run-callback" + ) + (stored,) = storage.written.values() + assert ( + callback["metrics"]["report_sha256"] + == hashlib.sha256(report_bytes(stored)).hexdigest() + ) def test_guard_callback_refuses_everything_off_the_list(): @@ -314,6 +327,8 @@ def test_guard_callback_refuses_everything_off_the_list(): {**base, "status": "VERIFIED by Alice"}, {**base, "outcome": {"outcome": "VERIFIED", "note": "Alice"}}, {**base, "metrics": {"wrap_readback": {"readback_sha256": "x"}}}, + {**base, "metrics": {"report_sha256": f"report of {CANARY_RECORD}"}}, + {**base, "metrics": {"report_sha256": "A" * 64}}, ] for body in bad: with pytest.raises(egress.EgressViolation): @@ -842,6 +857,29 @@ def body(envelope): assert CANARY_RECORD not in json.dumps(sent) +def test_local_report_digest_matches_the_stored_bytes(tmp_path): + from openadapt_flow.connector.storage import LocalCustomerStorage, report_bytes + + report = {"success": True, "note": CANARY_RECORD} + storage = LocalCustomerStorage(str(tmp_path)) + written = storage.write_report("org_1/run_1/report.json", report) + assert Path(written).read_bytes() == report_bytes(report) + assert egress.guard_callback( + { + "run_id": "run_1", + "org_id": "org_1", + "workflow_id": "wf_1", + "status": "success", + "report_path": "org_1/run_1/report.json", + "metrics": { + "steps": 1, + "report_sha256": hashlib.sha256(report_bytes(report)).hexdigest(), + }, + "mode": "replay", + } + )["metrics"]["report_sha256"] + + def test_wrap_readback_evidence_reads_the_v1_effect_receipt(): """verify-run nests the closed labels under "receipt" (effect-receipt/v1).""" from openadapt_flow.connector.executor import wrap_readback_evidence