Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/flow/openadapt_flow/connector/egress.py
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion packages/flow/openadapt_flow/connector/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion packages/flow/openadapt_flow/connector/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions packages/flow/tests/test_connector_pilot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand Down
Loading