The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(unbroker): close in-flight cases as not_found only for defunct brokers
kshitijk4poor24170bb3f402
diff --git a/optional-skills/security/unbroker/scripts/autopilot.py b/optional-skills/security/unbroker/scripts/autopilot.pyindex dfefa6b5353..58ed9eda239 100644--- a/optional-skills/security/unbroker/scripts/autopilot.py+++ b/optional-skills/security/unbroker/scripts/autopilot.py@@ -121,18 +121,21 @@ 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++def _defunct_closeout_digest(ledger: dict, defunct_ids: set, by_id: dict, subject_id: str) -> list[dict]:"""One-time digest entries closing the OPEN cases of defunct brokers as not_found.The plan and batch views exclude dead domains (never scanned, opted-out, or re-checked),so this is the only place they surface -- and not_found from an in-flight state is legal- only with the defunct evidence cited (see ledger.TRANSITIONS).+ only for a broker whose record carries `defunct` (see ledger.DEFUNCT_CLOSEOUT_FROM)."""out = []for bid, case in sorted(ledger.items()):- if bid not in defunct_ids or case.get("state") not in {- "found", "action_selected", "submitted", "verification_pending",- "awaiting_processing", "human_task_queued"}:+ if bid not in defunct_ids or case.get("state") not in _DEFUNCT_OPEN_STATES:continueout.append(_digest({"broker_id": bid, "broker_name": (by_id.get(bid) or {}).get("name") or bid},diff --git a/optional-skills/security/unbroker/scripts/ledger.py b/optional-skills/security/unbroker/scripts/ledger.pyindex 337bccc39ed..e383d37db6d 100644--- a/optional-skills/security/unbroker/scripts/ledger.py+++ b/optional-skills/security/unbroker/scripts/ledger.py@@ -30,24 +30,18 @@ TRANSITIONS: dict[str, set[str]] = {# request (-> submitted) or a human task. Re-scan can clear it (-> not_found) or upgrade it to a# direct listing (-> found)."indirect_exposure": {"submitted", "human_task_queued", "not_found", "found", "blocked"},- "action_selected": {"submitted", "human_task_queued", "blocked", "not_found"},- # not_found from the in-flight states exists for ONE reason: the broker died (domain- # seized by court order / NXDOMAIN). A dead site cannot hold the listing, so the honest- # close-out is not_found citing the defunct record -- never used for a LIVE broker's- # in-flight request, which waits out its processing window instead.- "submitted": {"verification_pending", "awaiting_processing", "human_task_queued", "blocked",- "not_found"},+ "action_selected": {"submitted", "human_task_queued", "blocked"},+ "submitted": {"verification_pending", "awaiting_processing", "human_task_queued", "blocked"},# verification_pending -> awaiting_processing: the verify link was opened/acknowledged and the# broker is now processing the removal (their stated window). confirmed_removed still requires a# verifying re-scan, never the submission flow's own say-so.- "verification_pending": {"awaiting_processing", "confirmed_removed", "human_task_queued",- "blocked", "not_found"},- "awaiting_processing": {"confirmed_removed", "human_task_queued", "blocked", "not_found"},+ "verification_pending": {"awaiting_processing", "confirmed_removed", "human_task_queued", "blocked"},+ "awaiting_processing": {"confirmed_removed", "human_task_queued", "blocked"},"confirmed_removed": {"reappeared", "confirmed_removed"},"reappeared": {"found", "indirect_exposure"},"human_task_queued": {"found", "indirect_exposure", "action_selected", "submitted", "verification_pending",- "awaiting_processing", "confirmed_removed", "blocked", "not_found",+ "awaiting_processing", "confirmed_removed", "blocked",},# blocked: automated tools (web_extract/proxyless browser) couldn't read the site. A later pass# -- a stealth/cloud browser OR guiding the operator's own (residential) browser -- can resolve it@@ -58,6 +52,13 @@ TRANSITIONS: dict[str, set[str]] = {"human_task_queued"},}+# 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+# 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"})+def now() -> str:return _dt.datetime.now(_dt.UTC).strftime("%Y-%m-%dT%H:%M:%SZ")@@ -92,7 +93,8 @@ def can_transition(old: str, new: str) -> bool:return new == old or new in TRANSITIONS.get(old, set())-def transition(subject_id: str, broker_id: str, new_state: str, **fields) -> dict:+def transition(subject_id: str, broker_id: str, new_state: str, *, broker_defunct: bool = False,+ **fields) -> dict:if new_state not in STATES:raise ValueError(f"unknown state {new_state!r}")# Lock the whole load-modify-save so a concurrent cron re-scan / other tenant@@ -101,9 +103,14 @@ def transition(subject_id: str, broker_id: str, new_state: str, **fields) -> dicledger = load(subject_id)case = ledger.get(broker_id) or new_case(subject_id, broker_id)old = case.get("state", "new")- if not can_transition(old, new_state):+ 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):raise ValueError(f"illegal transition {old!r} -> {new_state!r} for broker {broker_id!r}")case["state"] = new_state+ if new_state == "not_found":+ # not_found schedules no follow-up; a recheck date left by the prior state would+ # keep `next` waking for a closed case.+ case.pop("next_recheck_at", None)for key, value in fields.items():case[key] = valuestamp = now()diff --git a/optional-skills/security/unbroker/scripts/pdd.py b/optional-skills/security/unbroker/scripts/pdd.pyindex a0d2f17e476..f1f2939eb3d 100644--- a/optional-skills/security/unbroker/scripts/pdd.py+++ b/optional-skills/security/unbroker/scripts/pdd.py@@ -588,7 +588,8 @@ def cmd_record(args) -> None:fields["evidence"] = json.loads(args.evidence)if args.reason:fields["human_task_reason"] = args.reason- case = ledger_mod.transition(args.subject, args.broker, args.state, **fields)+ case = ledger_mod.transition(args.subject, args.broker, args.state,+ broker_defunct=bool((broker or {}).get("defunct")), **fields)if args.disclosed:ledger_mod.log_disclosure(args.subject, args.broker, args.disclosed, args.channel or "unknown")_out({"broker": args.broker, "state": case["state"],diff --git a/tests/skills/test_unbroker_skill.py b/tests/skills/test_unbroker_skill.pyindex 98fca73a83d..f9011e6c2e3 100644--- a/tests/skills/test_unbroker_skill.py+++ b/tests/skills/test_unbroker_skill.py@@ -884,10 +884,24 @@ def test_defunct_brokers_excluded_from_all_planning():assert close and "not_found" in _json.dumps(close)-def test_ledger_allows_not_found_close_out_for_dead_brokers():- for st in ("found", "action_selected", "submitted", "verification_pending",- "awaiting_processing", "human_task_queued"):- assert "not_found" in ledger.TRANSITIONS[st], st+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]",+ "--consent"])["subject_id"]+ assert brokers.get("radaris").get("defunct") and not brokers.get("usphonebook").get("defunct")+ for bid in ("radaris", "usphonebook"):+ _run(["record", sid, bid, "found", "--found", f"https://{bid}.example/x"])+ assert _run(["record", sid, bid, "submitted"])["next_recheck_at"]+ # a LIVE broker's in-flight request cannot be closed as not_found ...+ try:+ _run(["record", sid, "usphonebook", "not_found"])+ except SystemExit as e:+ assert "illegal transition 'submitted' -> 'not_found'" in str(e.code)+ else:+ raise AssertionError("live broker submitted -> not_found must be refused")+ # ... a defunct one can, and the closed case schedules no further wake+ out = _run(["record", sid, "radaris", "not_found", "--reason", "broker defunct"])+ assert out["state"] == "not_found" and out["next_recheck_at"] is Nonedef test_focus_phone_edge_cases():