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: 3 additions & 3 deletions packages/flow/docs/RUN_LEDGER.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,10 @@ refused) replaces calls and cost with the proxy's count (`metered` when its

| `status` (`path: agent_fallback`) | Bucket (reason) |
| --- | --- |
| VERIFIED with P7's `verified` and a record check that is `readable: true` and passed | VERIFIED (`readback_verified`, independent: the record check read the system of record before and after) |
| VERIFIED, but the record check isn't `readable: true` (a failed read, or an older check without the flag) | UNKNOWN (`reconciliation_required`) |
| VERIFIED with P7's `verified`, a record check that is `readable: true` and passed, and P7's own record (`fallback.status`, and `run_status` when kept) saying VERIFIED | VERIFIED (`readback_verified`, independent: the record check read the system of record before and after) |
| VERIFIED, but the record check isn't `readable: true` (a failed read, or an older check without the flag), or P7's own status or run_status says otherwise | UNKNOWN (`reconciliation_required`) |
| GATE_REFUSED, record check `readable: true` with no write | ESCALATED (`person_declined`) |
| GATE_REFUSED or COST_CAP_EXCEEDED, record check missing, not `readable: true`, or showing a write | UNKNOWN (`write_unconfirmed`). An unreadable store also reports zero counts, so zeros alone prove nothing. |
| GATE_REFUSED or COST_CAP_EXCEEDED, record check missing, not `readable: true`, or showing a write | UNKNOWN (`write_unconfirmed`). An unreadable store also reports zero counts, so zeros alone prove nothing. "No write" also needs every `outside_run` count zero, as P7's `_nothing_written` does: a change to another record during the run may be the run's own write, and a check without `outside_run` proves nothing. |
| COST_CAP_EXCEEDED, no write | FAILED (`policy_rejected_no_write`) |
| GATE_NOT_PROVED | UNKNOWN (`self_approved`) |
| RECONCILIATION_REQUIRED | UNKNOWN (`reconciliation_required`) |
Expand Down
4 changes: 4 additions & 0 deletions packages/flow/openadapt_flow/run_ledger/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,10 +624,14 @@ def collect_agent_run_record(path: Path) -> Contribution:
if isinstance(policy, dict) and "task_record_check" in policy
else result.get("record_check")
)
from openadapt_flow.run_ledger.readers import policy_agrees

fallback_unproved = not (
isinstance(check, dict)
and check.get("readable") is True
and check.get("passed") is True
and isinstance(policy, dict)
and policy_agrees(policy)
)
out.executor_kind = (
"program" if path_kind == "skill" else "agent" if path_kind == "agent" else None
Expand Down
32 changes: 25 additions & 7 deletions packages/flow/openadapt_flow/run_ledger/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,17 +345,34 @@ def is_dispatch_result(doc: Any) -> bool:

def _nothing_written(check: Any) -> bool:
"""P7's ``_nothing_written``: both reads of the record check succeeded
(``readable: true``) and saw no change. An unreadable store also reports
zero counts, so a check without ``readable: true`` (an older one, or a
failed read) proves nothing."""
(``readable: true``) and saw no change at all. An unreadable store also
reports zero counts, so a check without ``readable: true`` (an older one,
or a failed read) proves nothing. A change to another record during the
run (``outside_run``) may be the run's own write elsewhere, so it counts
as something written, and so does a check without ``outside_run``."""

def zero(counts: Any) -> bool:
return isinstance(counts, Mapping) and all(
_int(counts.get(key)) == 0 and not isinstance(counts.get(key), bool)
for key in ("created", "changed", "removed")
)

return (
isinstance(check, Mapping)
and check.get("readable") is True
and all(
_int(check.get(key)) == 0 and not isinstance(check.get(key), bool)
for key in ("created", "changed", "removed")
)
and zero(check)
and zero(check.get("outside_run"))
)


def policy_agrees(fallback: Mapping[str, Any]) -> bool:
"""P7's own record of the fallback says VERIFIED: its ``status``, and its
``run_status`` when it keeps one. Anything else (a missing record, a
RECONCILIATION_REQUIRED P7 put there) doesn't confirm the save."""

return (
fallback.get("status") == "VERIFIED"
and fallback.get("run_status", "VERIFIED") == "VERIFIED"
)


Expand Down Expand Up @@ -470,6 +487,7 @@ def _dict(value: Any) -> dict[str, Any]:
isinstance(check, Mapping)
and check.get("readable") is True
and check.get("passed") is True
and policy_agrees(fallback)
)
reason = fallback_reason(
status,
Expand Down
73 changes: 73 additions & 0 deletions packages/flow/tests/test_run_ledger_joins.py
Original file line number Diff line number Diff line change
Expand Up @@ -1063,3 +1063,76 @@ def test_export_checks_source_paths_like_labels(tmp_path):
assert any(part.startswith("src-") for s in sources for part in s.split("/"))
# The real paths stay in the local row.
assert "result-drift-approved.json" in store.rows()[0].sources


# -- P7's outside_run counts and its own status (after #2478) -----------------------

_RUN3_RESULTS = (
RUN3_FALLBACK / "cache" / "fallback" / "wrap.fallback-e28afecc2347807a-1035f74ad522"
)
_RUN3_DECLINED = _RUN3_RESULTS / "fallback-26d212a3c6483cd967c68cb19948794c25abc33d"
_RUN3_APPROVED = _RUN3_RESULTS / "fallback-ca9527beac76e04f400612a30e06cb3373c2914b"


def _edited_result(tmp_path: Path, source: Path, edit) -> Path:
run = tmp_path / source.name
run.mkdir()
doc = json.loads((source / "result.json").read_text())
edit(doc)
(run / "result.json").write_text(json.dumps(doc))
return run


def test_the_real_run3_fallback_results():
row, _, _ = build_row(_RUN3_APPROVED)
assert _outcome(row) == ("VERIFIED", "readback_verified")
row, _, _ = build_row(_RUN3_DECLINED)
assert _outcome(row) == ("ESCALATED", "person_declined")


@pytest.mark.parametrize(
"outside",
[{"created": 0, "changed": 1, "removed": 0}, None],
ids=["a-write-elsewhere", "no-outside-run"],
)
def test_a_decline_with_an_outside_run_write_is_not_no_write(tmp_path, outside):
# P7 counts a change to another record during the run, or a check with
# no outside_run, as a possible write: never "nothing was written".
def edit(doc):
check = doc["replay"]["fallback"]["task_record_check"]
if outside is None:
check.pop("outside_run", None)
else:
check["outside_run"] = outside

row, _, _ = build_row(_edited_result(tmp_path, _RUN3_DECLINED, edit))
assert _outcome(row) == ("UNKNOWN", "write_unconfirmed")


@pytest.mark.parametrize(
("key", "value"),
[("status", "RECONCILIATION_REQUIRED"), ("run_status", "GATE_NOT_PROVED")],
)
def test_a_verified_fallback_needs_p7s_own_status_to_agree(tmp_path, key, value):
def edit(doc):
doc["replay"]["fallback"][key] = value

row, _, _ = build_row(_edited_result(tmp_path, _RUN3_APPROVED, edit))
assert _outcome(row) == ("UNKNOWN", "reconciliation_required")


def test_a_wrap_record_needs_p7s_own_status_to_agree(tmp_path):
record = (
RUN3_FALLBACK
/ "runs"
/ "drift-approved"
/ "openadapt-agent"
/ "runs"
/ "run-b480e452b3ba49aab785d2ca.json"
)
doc = json.loads(record.read_text())
doc["result"]["fallback"]["status"] = "RECONCILIATION_REQUIRED"
path = tmp_path / record.name
path.write_text(json.dumps(doc))
row, _, _ = build_row(path)
assert _outcome(row) == ("UNKNOWN", "reconciliation_required")
Loading