The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(unbroker): dead brokers stay dead on --priority runs and never set a wake
kshitijk4poorf33e4811274e
diff --git a/optional-skills/security/unbroker/scripts/autopilot.py b/optional-skills/security/unbroker/scripts/autopilot.pyindex 30c18dbfb1d..1288187d1e4 100644--- a/optional-skills/security/unbroker/scripts/autopilot.py+++ b/optional-skills/security/unbroker/scripts/autopilot.py@@ -53,9 +53,9 @@ def _now_iso() -> str:return _dt.datetime.now(_dt.UTC).strftime("%Y-%m-%dT%H:%M:%SZ")-def _min_future_recheck(ledger: dict, at: str) -> str | None:- future = [c.get("next_recheck_at") for c in ledger.values()- if c.get("next_recheck_at") and c["next_recheck_at"] > at]+def _min_future_recheck(ledger: dict, at: str, skip: set | frozenset = frozenset()) -> str | None:+ future = [c.get("next_recheck_at") for bid, c in ledger.items()+ if bid not in skip and c.get("next_recheck_at") and c["next_recheck_at"] > at]return min(future) if future else None@@ -122,9 +122,11 @@ def _email_identity_ready(dossier: dict) -> bool:and dossier_mod.contact_email(dossier))-# Open defunct cases worth closing: the states with an ordinary not_found edge plus the-# in-flight states the ledger lets a defunct broker close from.-_DEFUNCT_OPEN_STATES = {"found", "blocked", "indirect_exposure"} | ledger_mod.DEFUNCT_CLOSEOUT_FROM+# Open defunct cases worth closing: every state the ledger lets a defunct broker leave for+# not_found, minus the pre-verdict ones (new/searching) that never hold a listing.+_DEFUNCT_OPEN_STATES = {s for s in ledger_mod.STATES+ if s not in {"new", "searching", "not_found"}+ and ledger_mod.can_transition(s, "not_found", broker_defunct=True)}def _defunct_closeout_digest(ledger: dict, defunct_ids: set, by_id: dict, subject_id: str) -> list[dict]:@@ -355,7 +357,9 @@ def next_actions(dossier: dict, brokers_list: list[dict], cfg: dict,groups = batch["groups"]playbook = {p["broker_id"]: p for p in batch.get("parent_playbook") or []}by_id = {b.get("id"): b for b in brokers_list}- defunct_ids = {b.get("id") for b in brokers_list if b.get("defunct")}+ # From the whole catalog, not just brokers_list: `next --priority` passes a filtered list+ # but the ledger loops below walk every case.+ defunct_ids = {b.get("id") for b in [*brokers_list, *brokers_mod.load_all()] if b.get("defunct")}actions: list[dict] = []digest: list[dict] = []@@ -506,7 +510,7 @@ def next_actions(dossier: dict, brokers_list: list[dict], cfg: dict,elif drop_filed:coverage["note"] = "DROP already filed; registry deletions are in the brokers' hands."- next_wake = _min_future_recheck(ledger, at)+ next_wake = _min_future_recheck(ledger, at, skip=defunct_ids) # a dead case never wakes usreturn {"subject": subject_id,"autonomy": autonomy,diff --git a/optional-skills/security/unbroker/scripts/ledger.py b/optional-skills/security/unbroker/scripts/ledger.pyindex e383d37db6d..92c52584b50 100644--- a/optional-skills/security/unbroker/scripts/ledger.py+++ b/optional-skills/security/unbroker/scripts/ledger.py@@ -54,7 +54,7 @@ TRANSITIONS: dict[str, set[str]] = {# A defunct broker (structured `defunct` field in its record: domain seized / NXDOMAIN) cannot# hold a listing, so its open case may close as not_found from an in-flight state. Gated on the-# broker record in `transition(..., broker_defunct=True)`: a LIVE broker's in-flight request+# broker record via `can_transition(..., broker_defunct=True)`: a LIVE broker's in-flight request# waits out its processing window and is confirmed by a verifying re-scan instead.DEFUNCT_CLOSEOUT_FROM = frozenset({"action_selected", "submitted", "verification_pending","awaiting_processing", "human_task_queued"})@@ -89,8 +89,10 @@ def get_case(subject_id: str, broker_id: str) -> dict:return load(subject_id).get(broker_id) or new_case(subject_id, broker_id)-def can_transition(old: str, new: str) -> bool:- return new == old or new in TRANSITIONS.get(old, set())+def can_transition(old: str, new: str, *, broker_defunct: bool = False) -> bool:+ if new == old or new in TRANSITIONS.get(old, set()):+ return True+ return broker_defunct and new == "not_found" and old in DEFUNCT_CLOSEOUT_FROMdef transition(subject_id: str, broker_id: str, new_state: str, *, broker_defunct: bool = False,@@ -103,8 +105,7 @@ def transition(subject_id: str, broker_id: str, new_state: str, *, broker_defuncledger = load(subject_id)case = ledger.get(broker_id) or new_case(subject_id, broker_id)old = case.get("state", "new")- defunct_closeout = broker_defunct and new_state == "not_found" and old in DEFUNCT_CLOSEOUT_FROM- if not (can_transition(old, new_state) or defunct_closeout):+ if not can_transition(old, new_state, broker_defunct=broker_defunct):raise ValueError(f"illegal transition {old!r} -> {new_state!r} for broker {broker_id!r}")case["state"] = new_stateif new_state == "not_found":diff --git a/tests/skills/test_unbroker_skill.py b/tests/skills/test_unbroker_skill.pyindex da4c5b37900..8cad9924073 100644--- a/tests/skills/test_unbroker_skill.py+++ b/tests/skills/test_unbroker_skill.py@@ -889,6 +889,11 @@ def test_defunct_brokers_excluded_from_all_planning():q2 = autopilot.next_actions(d, bl, _auto_cfg(), led, env={})parked = [t for t in q2["human_digest"] if "deadbroker" in _json.dumps(t)]assert len(parked) == 1 and "not_found" in _json.dumps(parked)+ # blocked / indirect_exposure dead cases are hidden from planning, so they close out too+ for st in ("blocked", "indirect_exposure"):+ led["deadbroker"] = {"broker_id": "deadbroker", "state": st}+ q3 = autopilot.next_actions(d, bl, _auto_cfg(), led, env={})+ assert [t for t in q3["human_digest"] if "deadbroker" in _json.dumps(t)], stdef test_full_autonomy_note_honors_confirm_first():@@ -903,6 +908,24 @@ def test_full_autonomy_note_honors_confirm_first():assert "EXCEPT on an action with confirm_first=true" in q["note"]+def test_defunct_cases_never_recheck_or_wake_even_on_priority_runs():+ # radaris is curated, crucial and defunct; a --priority high run filters it out of the+ # broker list, but its ledger case must still be treated as dead.+ with temp_env():+ d = _consenting()+ bl = brokers.by_priority("high")+ assert "radaris" not in {b["id"] for b in bl} and brokers.get("radaris").get("defunct")+ led = {"radaris": {"broker_id": "radaris", "state": "confirmed_removed",+ "next_recheck_at": "2020-01-01T00:00:00Z"}}+ q = autopilot.next_actions(d, bl, _auto_cfg(), led, env={})+ assert not [a for a in q["actions"] if a.get("broker_id") == "radaris"]+ led["radaris"]["next_recheck_at"] = "2099-01-01T00:00:00Z"+ assert autopilot.next_actions(d, bl, _auto_cfg(), led, env={})["next_wake_at"] is None+ led["radaris"] = {"broker_id": "radaris", "state": "awaiting_processing"}+ q2 = autopilot.next_actions(d, bl, _auto_cfg(), led, env={})+ assert [t for t in q2["human_digest"] if "radaris" in _json.dumps(t)]++def test_ledger_not_found_close_out_only_for_defunct_brokers():with temp_env():sid = _run(["intake", "--full-name", "Jane Q. Public", "--email", "[email protected]",@@ -921,6 +944,9 @@ def test_ledger_not_found_close_out_only_for_defunct_brokers():# ... a defunct one can, and the closed case schedules no further wakeout = _run(["record", sid, "radaris", "not_found", "--reason", "broker defunct"])assert out["state"] == "not_found" and out["next_recheck_at"] is None+ # the defunct flag widens only in-flight -> not_found+ assert not ledger.can_transition("confirmed_removed", "not_found", broker_defunct=True)+ assert not ledger.can_transition("submitted", "found", broker_defunct=True)def test_focus_phone_edge_cases():