|
| 1 | +"""Boot-time data integrity repairs for modern storage payloads. |
| 2 | +
|
| 3 | +These repairs are not schema migrations. They normalize impossible runtime |
| 4 | +state that may enter storage through historic bugs, imports, or interrupted |
| 5 | +write sequences. Repairs in this module must be: |
| 6 | +
|
| 7 | +- idempotent |
| 8 | +- safe to run on every startup |
| 9 | +- named by invariant, not by incident or ticket number |
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +from typing import Any |
| 15 | + |
| 16 | +from custom_components.choreops import const |
| 17 | +from custom_components.choreops.engines.chore_engine import ChoreEngine |
| 18 | + |
| 19 | + |
| 20 | +def run_boot_repairs(data: dict[str, Any]) -> dict[str, dict[str, int]]: |
| 21 | + """Run all modern boot repairs and return per-repair summaries.""" |
| 22 | + return { |
| 23 | + "repair_impossible_due_state_residue": repair_impossible_due_state_residue(data) |
| 24 | + } |
| 25 | + |
| 26 | + |
| 27 | +def repair_impossible_due_state_residue(data: dict[str, Any]) -> dict[str, int]: |
| 28 | + """Clear impossible overdue residue when no active due date exists.""" |
| 29 | + summary = { |
| 30 | + "chores_sanitized": 0, |
| 31 | + "stale_due_dates_cleared": 0, |
| 32 | + "assignee_states_normalized": 0, |
| 33 | + "global_states_normalized": 0, |
| 34 | + } |
| 35 | + |
| 36 | + chores_raw = data.get(const.DATA_CHORES) |
| 37 | + users_raw = data.get(const.DATA_USERS) |
| 38 | + if not isinstance(chores_raw, dict) or not isinstance(users_raw, dict): |
| 39 | + return summary |
| 40 | + |
| 41 | + for chore_id, chore_value in chores_raw.items(): |
| 42 | + if not isinstance(chore_value, dict): |
| 43 | + continue |
| 44 | + |
| 45 | + chore_data: dict[str, Any] = chore_value |
| 46 | + chore_changed = False |
| 47 | + uses_chore_level_due_date = ChoreEngine.uses_chore_level_due_date(chore_data) |
| 48 | + due_date_raw = chore_data.get(const.DATA_CHORE_DUE_DATE) |
| 49 | + per_assignee_due_dates_raw = chore_data.get( |
| 50 | + const.DATA_CHORE_PER_ASSIGNEE_DUE_DATES, {} |
| 51 | + ) |
| 52 | + per_assignee_due_dates = ( |
| 53 | + per_assignee_due_dates_raw |
| 54 | + if isinstance(per_assignee_due_dates_raw, dict) |
| 55 | + else {} |
| 56 | + ) |
| 57 | + |
| 58 | + if not due_date_raw and uses_chore_level_due_date and per_assignee_due_dates: |
| 59 | + cleared_count = sum( |
| 60 | + 1 for due_date in per_assignee_due_dates.values() if due_date |
| 61 | + ) |
| 62 | + if cleared_count > 0: |
| 63 | + for assignee_id in list(per_assignee_due_dates): |
| 64 | + per_assignee_due_dates[assignee_id] = None |
| 65 | + summary["stale_due_dates_cleared"] += cleared_count |
| 66 | + chore_changed = True |
| 67 | + |
| 68 | + has_active_due_date = ( |
| 69 | + bool(due_date_raw) |
| 70 | + if uses_chore_level_due_date |
| 71 | + else any( |
| 72 | + due_date for due_date in per_assignee_due_dates.values() if due_date |
| 73 | + ) |
| 74 | + ) |
| 75 | + if has_active_due_date: |
| 76 | + if chore_changed: |
| 77 | + summary["chores_sanitized"] += 1 |
| 78 | + continue |
| 79 | + |
| 80 | + assignee_ids_raw = chore_data.get(const.DATA_CHORE_ASSIGNED_USER_IDS, []) |
| 81 | + assignee_ids = assignee_ids_raw if isinstance(assignee_ids_raw, list) else [] |
| 82 | + assignee_states: dict[str, str] = {} |
| 83 | + |
| 84 | + for assignee_id in assignee_ids: |
| 85 | + user_value = users_raw.get(assignee_id, {}) |
| 86 | + if not isinstance(user_value, dict): |
| 87 | + assignee_states[assignee_id] = const.CHORE_STATE_PENDING |
| 88 | + continue |
| 89 | + |
| 90 | + chore_tracking_raw = user_value.get(const.DATA_USER_CHORE_DATA, {}) |
| 91 | + chore_tracking = ( |
| 92 | + chore_tracking_raw if isinstance(chore_tracking_raw, dict) else {} |
| 93 | + ) |
| 94 | + assignee_chore_value = chore_tracking.get(chore_id, {}) |
| 95 | + assignee_chore_data = ( |
| 96 | + assignee_chore_value if isinstance(assignee_chore_value, dict) else {} |
| 97 | + ) |
| 98 | + |
| 99 | + current_state = assignee_chore_data.get( |
| 100 | + const.DATA_USER_CHORE_DATA_STATE, |
| 101 | + const.CHORE_STATE_PENDING, |
| 102 | + ) |
| 103 | + if current_state in ( |
| 104 | + const.CHORE_STATE_OVERDUE, |
| 105 | + const.CHORE_STATE_MISSED, |
| 106 | + ): |
| 107 | + assignee_chore_data[const.DATA_USER_CHORE_DATA_STATE] = ( |
| 108 | + const.CHORE_STATE_PENDING |
| 109 | + ) |
| 110 | + current_state = const.CHORE_STATE_PENDING |
| 111 | + summary["assignee_states_normalized"] += 1 |
| 112 | + chore_changed = True |
| 113 | + |
| 114 | + assignee_states[assignee_id] = ( |
| 115 | + current_state |
| 116 | + if isinstance(current_state, str) |
| 117 | + else const.CHORE_STATE_PENDING |
| 118 | + ) |
| 119 | + |
| 120 | + current_global_state = chore_data.get(const.DATA_CHORE_STATE) |
| 121 | + if current_global_state in ( |
| 122 | + const.CHORE_STATE_OVERDUE, |
| 123 | + const.CHORE_STATE_MISSED, |
| 124 | + ): |
| 125 | + normalized_global_state = ( |
| 126 | + ChoreEngine.compute_global_chore_state(chore_data, assignee_states) |
| 127 | + if assignee_states |
| 128 | + else const.CHORE_STATE_PENDING |
| 129 | + ) |
| 130 | + if current_global_state != normalized_global_state: |
| 131 | + chore_data[const.DATA_CHORE_STATE] = normalized_global_state |
| 132 | + summary["global_states_normalized"] += 1 |
| 133 | + chore_changed = True |
| 134 | + |
| 135 | + if chore_changed: |
| 136 | + summary["chores_sanitized"] += 1 |
| 137 | + |
| 138 | + return summary |
0 commit comments