def select_next(bundle: Path, completed_target: str, *, mode: str, anchor: str = "", interludes: int = 0) -> dict[str, str]: if mode != "backfill": return {"action": "noop", "reason": "not_backfill"} from ops import cloud_cycle _ticket(anchor, interludes) targets = approved_targets() journal = json.loads((bundle / "cloud-journal.json").read_text(encoding="utf-8")) if (journal.get("status") != "published" or type(journal.get("generation")) is not int or journal["generation"] <= 0 or any(not re.fullmatch(r"[a-f0-9]{64}", (journal.get(key) or {}).get("sha256", "")) for key in ("auth", "bundle"))): raise RuntimeError("Continuation requires durable published state and renewable login") if journal.get("cleanup_pending"): return {"action": "noop", "reason": "prior_storage_cleanup_pending"} runs = json.loads((bundle / "state.json").read_text(encoding="utf-8"))["runs"] record = runs.get(completed_target, {}) if anchor and anchor != completed_target: _validated_anchor(bundle, anchor, runs) if completed_target in targets: _validated_anchor(bundle, completed_target, runs) if anchor and targets.index(completed_target) < targets.index(anchor): raise RuntimeError("Historical continuation cannot move its anchor backwards") if not anchor or completed_target != anchor: anchor, interludes = completed_target, 0 else: if not anchor: return {"action": "noop", "reason": "completed_historical_anchor_required"} if (record.get("run_kind") not in {"weekly", "correction"} or record.get("status") != "complete" or record.get("phase") != "published" or record.get("live_verified_locales") != ["fr", "en"] or record.get("review_decision") in {"hold", "revise"}): raise RuntimeError("Priority interlude has not completed bilingual publication") number, digest = record.get("issue"), record.get("packet_sha256") if (type(number) is not int or number <= 0 or not isinstance(digest, str) or not re.fullmatch(r"[a-f0-9]{64}", digest) or cloud_cycle._runner(bundle, completed_target)._public_issue_identity(number) != (number, digest)): raise RuntimeError("Priority interlude differs from its published authority") interludes += 1 if interludes > MAX_PRIORITY_INTERLUDES: return {"action": "noop", "reason": "priority_interlude_limit"} remaining = [target for target in targets if not cloud_cycle._week_is_published(bundle, target)] if not remaining: return {"action": "noop", "reason": "historical_queue_complete"} next_target = remaining[0] validate_successor(anchor, next_target) # Existing routing owns recovery, filing, corrections, current-week priority # and the immutable pilot gate. Never skip any of these to continue history. selected = cloud_cycle.choose_cycle(bundle, "backfill") action, target = selected.get("action"), selected.get("target") pending = runs.get(target, {}) if (pending.get("review_decision") in {"hold", "revise"} or (str(action).startswith("prepare") and pending.get("status") == "failed")): return {"action": "noop", "reason": "held_or_failed_review_requires_attention"} if action == "prepare_backfill": if target != next_target or selected.get("run_kind") != "retrospective": raise RuntimeError("Selected continuation differs from the approved historical successor") elif action in {"prepare", "prepare_correction", "recover", "file"}: if target not in targets and interludes >= MAX_PRIORITY_INTERLUDES: return {"action": "noop", "reason": "priority_interlude_limit"} else: return {"action": "noop", "reason": "no_historical_continuation"} return {"action": "dispatch", "target": next_target, "anchor": anchor, "interludes": str(interludes)}