diff --git a/src/operator_trend_closure_forecast_reset_controls.py b/src/operator_trend_closure_forecast_reset_controls.py index 1c3443b..80aede6 100644 --- a/src/operator_trend_closure_forecast_reset_controls.py +++ b/src/operator_trend_closure_forecast_reset_controls.py @@ -3787,11 +3787,44 @@ def closure_forecast_reset_reentry_rebuild_churn_summary( return "No meaningful reset re-entry rebuild churn is active right now." -def closure_forecast_reset_reentry_rebuild_reentry_refresh_recovery_for_target( +class _RecoveryTierSpec(NamedTuple): + """Tier-specific literals for the reset-reentry refresh-recovery builder. + + The rebuild / rererestore refresh-recovery families share one algorithm and + differ only in the per-event reset/freshness keys, the recovery + next-tier + restore status tokens, their reason prose, and their output keys. + """ + + reset_key: str + freshness_key: str + restoring_confirmation: str + restoring_clearance: str + recovering_confirmation: str + recovering_clearance: str + restored_confirmation: str + restored_clearance: str + pending_confirmation: str + pending_clearance: str + reason_restored_confirmation: str + reason_restored_clearance: str + reason_blocked: str + reason_pending_confirmation: str + reason_pending_clearance: str + score_key: str + recovery_status_key: str + next_status_key: str + next_reason_key: str + path_key: str + reset_side_key: str + aligned_fresh_key: str + + +def _recovery_for_target_base( target: dict[str, Any], closure_forecast_events: list[dict[str, Any]], transition_history_meta: dict[str, Any], *, + spec: _RecoveryTierSpec, ordered_reset_reentry_events_for_target: Callable[ [dict[str, Any], list[dict[str, Any]]], list[dict[str, Any]] ], @@ -3799,25 +3832,25 @@ def closure_forecast_reset_reentry_rebuild_reentry_refresh_recovery_for_target( normalized_closure_forecast_direction: Callable[[str, float], str], clamp_round: Callable[..., float], closure_forecast_direction_majority: Callable[[list[str]], str], - target_specific_normalization_noise: Callable[[dict[str, Any], dict[str, Any]], bool], - closure_forecast_direction_reversing: Callable[[str, str], bool], - closure_forecast_reset_reentry_rebuild_reentry_refresh_path_label: Callable[ - [dict[str, Any]], str + target_specific_normalization_noise: Callable[ + [dict[str, Any], dict[str, Any]], bool ], - class_reset_reentry_rebuild_reentry_refresh_restore_window_runs: int, + closure_forecast_direction_reversing: Callable[[str, str], bool], + path_label: Callable[[dict[str, Any]], str], + window_runs: int, ) -> dict[str, Any]: matching_events = ordered_reset_reentry_events_for_target( target, closure_forecast_events, - )[:class_reset_reentry_rebuild_reentry_refresh_restore_window_runs] - recent_rebuild_reentry_reset_side = "none" + )[:window_runs] + recent_reset_side = "none" latest_reset_index: int | None = None for index, event in enumerate(matching_events): event_reset_side = closure_forecast_reset_side_from_status( - str(event.get("closure_forecast_reset_reentry_rebuild_reentry_reset_status", "none")) + str(event.get(spec.reset_key, "none")) ) if event_reset_side != "none": - recent_rebuild_reentry_reset_side = event_reset_side + recent_reset_side = event_reset_side latest_reset_index = index break @@ -3833,7 +3866,7 @@ def closure_forecast_reset_reentry_rebuild_reentry_refresh_recovery_for_target( ) if ( closure_forecast_reset_side_from_status( - str(event.get("closure_forecast_reset_reentry_rebuild_reentry_reset_status", "none")) + str(event.get(spec.reset_key, "none")) ) == "none" and direction == "neutral" @@ -3842,7 +3875,7 @@ def closure_forecast_reset_reentry_rebuild_reentry_refresh_recovery_for_target( continue relevant_events.append(event) directions.append(direction) - if len(relevant_events) > class_reset_reentry_rebuild_reentry_refresh_restore_window_runs: + if len(relevant_events) > window_runs: break if direction == "neutral": signal_strength = 0.0 @@ -3856,42 +3889,36 @@ def closure_forecast_reset_reentry_rebuild_reentry_refresh_recovery_for_target( "stale": 0.25, "insufficient-data": 0.10, }.get( - str( - event.get( - "closure_forecast_reset_reentry_rebuild_reentry_freshness_status", - "insufficient-data", - ) - ), + str(event.get(spec.freshness_key, "insufficient-data")), 0.10, ) - weight = (1.0, 0.8, 0.6, 0.4)[ - min( - len(relevant_events) - 1, - class_reset_reentry_rebuild_reentry_refresh_restore_window_runs - 1, - ) - ] + weight = (1.0, 0.8, 0.6, 0.4)[min(len(relevant_events) - 1, window_runs - 1)] weighted_total += sign * signal_strength * freshness_factor * weight weight_sum += weight - recovery_score = clamp_round(weighted_total / max(weight_sum, 1.0), lower=-0.95, upper=0.95) + recovery_score = clamp_round( + weighted_total / max(weight_sum, 1.0), lower=-0.95, upper=0.95 + ) current_score = float(target.get("closure_forecast_reweight_score", 0.0) or 0.0) current_direction = normalized_closure_forecast_direction( str(target.get("closure_forecast_reweight_direction", "neutral")), current_score, ) - current_freshness = str( - target.get("closure_forecast_reset_reentry_rebuild_reentry_freshness_status", "insufficient-data") + current_freshness = str(target.get(spec.freshness_key, "insufficient-data")) + current_momentum = str( + target.get("closure_forecast_momentum_status", "insufficient-data") ) - current_momentum = str(target.get("closure_forecast_momentum_status", "insufficient-data")) current_stability = str(target.get("closure_forecast_stability_status", "watch")) earlier_majority = closure_forecast_direction_majority(directions[1:]) local_noise = target_specific_normalization_noise(target, transition_history_meta) - direction_reversing = closure_forecast_direction_reversing(current_direction, earlier_majority) + direction_reversing = closure_forecast_direction_reversing( + current_direction, earlier_majority + ) opposes_reset = ( - recent_rebuild_reentry_reset_side == "confirmation" + recent_reset_side == "confirmation" and current_direction == "supporting-clearance" ) or ( - recent_rebuild_reentry_reset_side == "clearance" + recent_reset_side == "clearance" and current_direction == "supporting-confirmation" ) aligned_fresh_runs_after_reset = 0 @@ -3910,12 +3937,8 @@ def closure_forecast_reset_reentry_rebuild_reentry_refresh_recovery_for_target( else "none" ) if ( - event_side == recent_rebuild_reentry_reset_side - and event.get( - "closure_forecast_reset_reentry_rebuild_reentry_freshness_status", - "insufficient-data", - ) - == "fresh" + event_side == recent_reset_side + and event.get(spec.freshness_key, "insufficient-data") == "fresh" ): aligned_fresh_runs_after_reset += 1 current_side = ( @@ -3927,122 +3950,201 @@ def closure_forecast_reset_reentry_rebuild_reentry_refresh_recovery_for_target( ) current_event_already_counted = any( event.get("generated_at", "") == "" - and float(event.get("closure_forecast_reweight_score", 0.0) or 0.0) == current_score + and float(event.get("closure_forecast_reweight_score", 0.0) or 0.0) + == current_score and event.get("closure_forecast_reweight_direction", "neutral") == target.get("closure_forecast_reweight_direction", "neutral") for event in matching_events[: latest_reset_index or 0] ) if ( - current_side == recent_rebuild_reentry_reset_side + current_side == recent_reset_side and current_freshness == "fresh" and not current_event_already_counted ): aligned_fresh_runs_after_reset += 1 - if len(relevant_events) < 2 or recent_rebuild_reentry_reset_side == "none": + if len(relevant_events) < 2 or recent_reset_side == "none": recovery_status = "none" elif local_noise and current_direction == "supporting-confirmation": recovery_status = "blocked" elif opposes_reset or direction_reversing: recovery_status = "reversing" elif ( - recent_rebuild_reentry_reset_side == "confirmation" + recent_reset_side == "confirmation" and current_direction == "supporting-confirmation" and current_freshness == "fresh" and recovery_score >= 0.25 and current_stability != "oscillating" ): - recovery_status = "restoring-confirmation-rebuild-reentry" + recovery_status = spec.restoring_confirmation elif ( - recent_rebuild_reentry_reset_side == "clearance" + recent_reset_side == "clearance" and current_direction == "supporting-clearance" and current_freshness == "fresh" and recovery_score <= -0.25 and current_stability != "oscillating" ): - recovery_status = "restoring-clearance-rebuild-reentry" + recovery_status = spec.restoring_clearance elif ( - recent_rebuild_reentry_reset_side == "confirmation" + recent_reset_side == "confirmation" and current_direction == "supporting-confirmation" and current_freshness in {"fresh", "mixed-age"} and recovery_score >= 0.15 ): - recovery_status = "recovering-confirmation-rebuild-reentry-reset" + recovery_status = spec.recovering_confirmation elif ( - recent_rebuild_reentry_reset_side == "clearance" + recent_reset_side == "clearance" and current_direction == "supporting-clearance" and current_freshness in {"fresh", "mixed-age"} and recovery_score <= -0.15 ): - recovery_status = "recovering-clearance-rebuild-reentry-reset" + recovery_status = spec.recovering_clearance else: recovery_status = "none" if ( - recovery_status == "restoring-confirmation-rebuild-reentry" + recovery_status == spec.restoring_confirmation and current_freshness == "fresh" and current_momentum == "sustained-confirmation" and current_stability == "stable" and not local_noise and aligned_fresh_runs_after_reset >= 2 ): - restore_status = "restored-confirmation-rebuild-reentry" - restore_reason = "Fresh confirmation-side follow-through has restored stronger rebuilt re-entry posture." + next_status = spec.restored_confirmation + next_reason = spec.reason_restored_confirmation elif ( - recovery_status == "restoring-clearance-rebuild-reentry" + recovery_status == spec.restoring_clearance and current_freshness == "fresh" and current_momentum == "sustained-clearance" and current_stability == "stable" and aligned_fresh_runs_after_reset >= 2 ): - restore_status = "restored-clearance-rebuild-reentry" - restore_reason = "Fresh clearance-side pressure has restored stronger rebuilt re-entry posture." + next_status = spec.restored_clearance + next_reason = spec.reason_restored_clearance elif local_noise and recovery_status in { - "recovering-confirmation-rebuild-reentry-reset", - "restoring-confirmation-rebuild-reentry", + spec.recovering_confirmation, + spec.restoring_confirmation, "blocked", }: - restore_status = "blocked" - restore_reason = ( - "Local target instability is still preventing positive confirmation-side rebuilt re-entry restore." - ) + next_status = "blocked" + next_reason = spec.reason_blocked elif recovery_status in { - "recovering-confirmation-rebuild-reentry-reset", - "restoring-confirmation-rebuild-reentry", + spec.recovering_confirmation, + spec.restoring_confirmation, }: - restore_status = "pending-confirmation-rebuild-reentry-restore" - restore_reason = ( - "Fresh confirmation-side evidence is returning after rebuilt re-entry was softened or " - "reset, but it has not yet restored stronger rebuilt re-entry posture." - ) + next_status = spec.pending_confirmation + next_reason = spec.reason_pending_confirmation elif recovery_status in { - "recovering-clearance-rebuild-reentry-reset", - "restoring-clearance-rebuild-reentry", + spec.recovering_clearance, + spec.restoring_clearance, }: - restore_status = "pending-clearance-rebuild-reentry-restore" - restore_reason = ( - "Fresh clearance-side evidence is returning after rebuilt re-entry was softened or " - "reset, but it has not yet restored stronger rebuilt re-entry posture." - ) + next_status = spec.pending_clearance + next_reason = spec.reason_pending_clearance else: - restore_status = "none" - restore_reason = "" + next_status = "none" + next_reason = "" return { - "closure_forecast_reset_reentry_rebuild_reentry_refresh_recovery_score": recovery_score, - "closure_forecast_reset_reentry_rebuild_reentry_refresh_recovery_status": recovery_status, - "closure_forecast_reset_reentry_rebuild_reentry_restore_status": restore_status, - "closure_forecast_reset_reentry_rebuild_reentry_restore_reason": restore_reason, - "recent_reset_reentry_rebuild_reentry_refresh_path": " -> ".join( - closure_forecast_reset_reentry_rebuild_reentry_refresh_path_label(event) - for event in matching_events - if event + spec.score_key: recovery_score, + spec.recovery_status_key: recovery_status, + spec.next_status_key: next_status, + spec.next_reason_key: next_reason, + spec.path_key: " -> ".join( + path_label(event) for event in matching_events if event ), - "recent_rebuild_reentry_reset_side": recent_rebuild_reentry_reset_side, - "aligned_fresh_runs_after_latest_rebuild_reentry_reset": aligned_fresh_runs_after_reset, + spec.reset_side_key: recent_reset_side, + spec.aligned_fresh_key: aligned_fresh_runs_after_reset, } +_REBUILD_RECOVERY_SPEC = _RecoveryTierSpec( + reset_key="closure_forecast_reset_reentry_rebuild_reentry_reset_status", + freshness_key="closure_forecast_reset_reentry_rebuild_reentry_freshness_status", + restoring_confirmation="restoring-confirmation-rebuild-reentry", + restoring_clearance="restoring-clearance-rebuild-reentry", + recovering_confirmation="recovering-confirmation-rebuild-reentry-reset", + recovering_clearance="recovering-clearance-rebuild-reentry-reset", + restored_confirmation="restored-confirmation-rebuild-reentry", + restored_clearance="restored-clearance-rebuild-reentry", + pending_confirmation="pending-confirmation-rebuild-reentry-restore", + pending_clearance="pending-clearance-rebuild-reentry-restore", + reason_restored_confirmation="Fresh confirmation-side follow-through has restored stronger rebuilt re-entry posture.", + reason_restored_clearance="Fresh clearance-side pressure has restored stronger rebuilt re-entry posture.", + reason_blocked="Local target instability is still preventing positive confirmation-side rebuilt re-entry restore.", + reason_pending_confirmation="Fresh confirmation-side evidence is returning after rebuilt re-entry was softened or reset, but it has not yet restored stronger rebuilt re-entry posture.", + reason_pending_clearance="Fresh clearance-side evidence is returning after rebuilt re-entry was softened or reset, but it has not yet restored stronger rebuilt re-entry posture.", + score_key="closure_forecast_reset_reentry_rebuild_reentry_refresh_recovery_score", + recovery_status_key="closure_forecast_reset_reentry_rebuild_reentry_refresh_recovery_status", + next_status_key="closure_forecast_reset_reentry_rebuild_reentry_restore_status", + next_reason_key="closure_forecast_reset_reentry_rebuild_reentry_restore_reason", + path_key="recent_reset_reentry_rebuild_reentry_refresh_path", + reset_side_key="recent_rebuild_reentry_reset_side", + aligned_fresh_key="aligned_fresh_runs_after_latest_rebuild_reentry_reset", +) + + +_RERERESTORE_RECOVERY_SPEC = _RecoveryTierSpec( + reset_key="closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_reset_status", + freshness_key="closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_freshness_status", + restoring_confirmation="rerererestoring-confirmation-rebuild-reentry", + restoring_clearance="rerererestoring-clearance-rebuild-reentry", + recovering_confirmation="recovering-confirmation-rebuild-reentry-rererestore-reset", + recovering_clearance="recovering-clearance-rebuild-reentry-rererestore-reset", + restored_confirmation="rerererestored-confirmation-rebuild-reentry", + restored_clearance="rerererestored-clearance-rebuild-reentry", + pending_confirmation="pending-confirmation-rebuild-reentry-rerererestore", + pending_clearance="pending-clearance-rebuild-reentry-rerererestore", + reason_restored_confirmation="Fresh confirmation-side follow-through has re-re-re-restored stronger re-re-restored rebuilt re-entry posture.", + reason_restored_clearance="Fresh clearance-side pressure has re-re-re-restored stronger re-re-restored rebuilt re-entry posture.", + reason_blocked="Local target instability is still preventing positive confirmation-side re-re-restored rebuilt re-entry re-re-re-restore.", + reason_pending_confirmation="Fresh confirmation-side evidence is returning after re-re-restored rebuilt re-entry softened or reset, but it has not yet re-re-re-restored stronger re-re-restored posture.", + reason_pending_clearance="Fresh clearance-side evidence is returning after re-re-restored rebuilt re-entry softened or reset, but it has not yet re-re-re-restored stronger re-re-restored posture.", + score_key="closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_refresh_recovery_score", + recovery_status_key="closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_refresh_recovery_status", + next_status_key="closure_forecast_reset_reentry_rebuild_reentry_restore_rerererestore_status", + next_reason_key="closure_forecast_reset_reentry_rebuild_reentry_restore_rerererestore_reason", + path_key="recent_reset_reentry_rebuild_reentry_restore_rererestore_refresh_path", + reset_side_key="recent_rererestore_reset_side", + aligned_fresh_key="aligned_fresh_runs_after_latest_rererestore_reset", +) + + +def closure_forecast_reset_reentry_rebuild_reentry_refresh_recovery_for_target( + target: dict[str, Any], + closure_forecast_events: list[dict[str, Any]], + transition_history_meta: dict[str, Any], + *, + ordered_reset_reentry_events_for_target: Callable[ + [dict[str, Any], list[dict[str, Any]]], list[dict[str, Any]] + ], + closure_forecast_reset_side_from_status: Callable[[str], str], + normalized_closure_forecast_direction: Callable[[str, float], str], + clamp_round: Callable[..., float], + closure_forecast_direction_majority: Callable[[list[str]], str], + target_specific_normalization_noise: Callable[[dict[str, Any], dict[str, Any]], bool], + closure_forecast_direction_reversing: Callable[[str, str], bool], + closure_forecast_reset_reentry_rebuild_reentry_refresh_path_label: Callable[ + [dict[str, Any]], str + ], + class_reset_reentry_rebuild_reentry_refresh_restore_window_runs: int, +) -> dict[str, Any]: + return _recovery_for_target_base( + target, + closure_forecast_events, + transition_history_meta, + spec=_REBUILD_RECOVERY_SPEC, + ordered_reset_reentry_events_for_target=ordered_reset_reentry_events_for_target, + closure_forecast_reset_side_from_status=closure_forecast_reset_side_from_status, + normalized_closure_forecast_direction=normalized_closure_forecast_direction, + clamp_round=clamp_round, + closure_forecast_direction_majority=closure_forecast_direction_majority, + target_specific_normalization_noise=target_specific_normalization_noise, + closure_forecast_direction_reversing=closure_forecast_direction_reversing, + path_label=closure_forecast_reset_reentry_rebuild_reentry_refresh_path_label, + window_runs=class_reset_reentry_rebuild_reentry_refresh_restore_window_runs, + ) + + def apply_reset_reentry_rebuild_reentry_refresh_restore_control( target: dict[str, Any], *, @@ -6732,279 +6834,21 @@ def closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_refresh_r ], class_reset_reentry_rebuild_reentry_restore_rererestore_refresh_window_runs: int, ) -> dict[str, Any]: - matching_events = ordered_reset_reentry_events_for_target( + return _recovery_for_target_base( target, closure_forecast_events, - )[:class_reset_reentry_rebuild_reentry_restore_rererestore_refresh_window_runs] - recent_rererestore_reset_side = "none" - latest_reset_index: int | None = None - for index, event in enumerate(matching_events): - event_reset_side = closure_forecast_reset_side_from_status( - str( - event.get( - "closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_reset_status", - "none", - ) - ) - ) - if event_reset_side != "none": - recent_rererestore_reset_side = event_reset_side - latest_reset_index = index - break - - relevant_events: list[dict[str, Any]] = [] - directions: list[str] = [] - weighted_total = 0.0 - weight_sum = 0.0 - for event in matching_events: - score = float(event.get("closure_forecast_reweight_score", 0.0) or 0.0) - direction = normalized_closure_forecast_direction( - str(event.get("closure_forecast_reweight_direction", "neutral")), - score, - ) - if ( - closure_forecast_reset_side_from_status( - str( - event.get( - "closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_reset_status", - "none", - ) - ) - ) - == "none" - and direction == "neutral" - and abs(score) < 0.05 - ): - continue - relevant_events.append(event) - directions.append(direction) - if ( - len(relevant_events) - > class_reset_reentry_rebuild_reentry_restore_rererestore_refresh_window_runs - ): - break - if direction == "neutral": - signal_strength = 0.0 - sign = 0.0 - else: - signal_strength = max(abs(score), 0.05) - sign = 1.0 if direction == "supporting-confirmation" else -1.0 - freshness_factor = { - "fresh": 1.00, - "mixed-age": 0.60, - "stale": 0.25, - "insufficient-data": 0.10, - }.get( - str( - event.get( - "closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_freshness_status", - "insufficient-data", - ) - ), - 0.10, - ) - weight = (1.0, 0.8, 0.6, 0.4)[ - min( - len(relevant_events) - 1, - class_reset_reentry_rebuild_reentry_restore_rererestore_refresh_window_runs - - 1, - ) - ] - weighted_total += sign * signal_strength * freshness_factor * weight - weight_sum += weight - - recovery_score = clamp_round( - weighted_total / max(weight_sum, 1.0), - lower=-0.95, - upper=0.95, - ) - current_score = float(target.get("closure_forecast_reweight_score", 0.0) or 0.0) - current_direction = normalized_closure_forecast_direction( - str(target.get("closure_forecast_reweight_direction", "neutral")), - current_score, - ) - current_freshness = str( - target.get( - "closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_freshness_status", - "insufficient-data", - ) - ) - current_momentum = str( - target.get("closure_forecast_momentum_status", "insufficient-data") - ) - current_stability = str(target.get("closure_forecast_stability_status", "watch")) - earlier_majority = closure_forecast_direction_majority(directions[1:]) - local_noise = target_specific_normalization_noise(target, transition_history_meta) - direction_reversing = closure_forecast_direction_reversing( - current_direction, - earlier_majority, - ) - opposes_reset = ( - recent_rererestore_reset_side == "confirmation" - and current_direction == "supporting-clearance" - ) or ( - recent_rererestore_reset_side == "clearance" - and current_direction == "supporting-confirmation" - ) - aligned_fresh_runs_after_reset = 0 - if latest_reset_index is not None and latest_reset_index > 0: - for event in matching_events[:latest_reset_index]: - score = float(event.get("closure_forecast_reweight_score", 0.0) or 0.0) - direction = normalized_closure_forecast_direction( - str(event.get("closure_forecast_reweight_direction", "neutral")), - score, - ) - event_side = ( - "confirmation" - if direction == "supporting-confirmation" - else "clearance" - if direction == "supporting-clearance" - else "none" - ) - if ( - event_side == recent_rererestore_reset_side - and event.get( - "closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_freshness_status", - "insufficient-data", - ) - == "fresh" - ): - aligned_fresh_runs_after_reset += 1 - current_side = ( - "confirmation" - if current_direction == "supporting-confirmation" - else "clearance" - if current_direction == "supporting-clearance" - else "none" - ) - current_event_already_counted = any( - event.get("generated_at", "") == "" - and float(event.get("closure_forecast_reweight_score", 0.0) or 0.0) - == current_score - and event.get("closure_forecast_reweight_direction", "neutral") - == target.get("closure_forecast_reweight_direction", "neutral") - for event in matching_events[: latest_reset_index or 0] + transition_history_meta, + spec=_RERERESTORE_RECOVERY_SPEC, + ordered_reset_reentry_events_for_target=ordered_reset_reentry_events_for_target, + closure_forecast_reset_side_from_status=closure_forecast_reset_side_from_status, + normalized_closure_forecast_direction=normalized_closure_forecast_direction, + clamp_round=clamp_round, + closure_forecast_direction_majority=closure_forecast_direction_majority, + target_specific_normalization_noise=target_specific_normalization_noise, + closure_forecast_direction_reversing=closure_forecast_direction_reversing, + path_label=closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_refresh_path_label, + window_runs=class_reset_reentry_rebuild_reentry_restore_rererestore_refresh_window_runs, ) - if ( - current_side == recent_rererestore_reset_side - and current_freshness == "fresh" - and not current_event_already_counted - ): - aligned_fresh_runs_after_reset += 1 - - if len(relevant_events) < 2 or recent_rererestore_reset_side == "none": - recovery_status = "none" - elif local_noise and current_direction == "supporting-confirmation": - recovery_status = "blocked" - elif opposes_reset or direction_reversing: - recovery_status = "reversing" - elif ( - recent_rererestore_reset_side == "confirmation" - and current_direction == "supporting-confirmation" - and current_freshness == "fresh" - and recovery_score >= 0.25 - and current_stability != "oscillating" - ): - recovery_status = "rerererestoring-confirmation-rebuild-reentry" - elif ( - recent_rererestore_reset_side == "clearance" - and current_direction == "supporting-clearance" - and current_freshness == "fresh" - and recovery_score <= -0.25 - and current_stability != "oscillating" - ): - recovery_status = "rerererestoring-clearance-rebuild-reentry" - elif ( - recent_rererestore_reset_side == "confirmation" - and current_direction == "supporting-confirmation" - and current_freshness in {"fresh", "mixed-age"} - and recovery_score >= 0.15 - ): - recovery_status = "recovering-confirmation-rebuild-reentry-rererestore-reset" - elif ( - recent_rererestore_reset_side == "clearance" - and current_direction == "supporting-clearance" - and current_freshness in {"fresh", "mixed-age"} - and recovery_score <= -0.15 - ): - recovery_status = "recovering-clearance-rebuild-reentry-rererestore-reset" - else: - recovery_status = "none" - - if ( - recovery_status == "rerererestoring-confirmation-rebuild-reentry" - and current_freshness == "fresh" - and current_momentum == "sustained-confirmation" - and current_stability == "stable" - and not local_noise - and aligned_fresh_runs_after_reset >= 2 - ): - rerererestore_status = "rerererestored-confirmation-rebuild-reentry" - rerererestore_reason = ( - "Fresh confirmation-side follow-through has re-re-re-restored stronger " - "re-re-restored rebuilt re-entry posture." - ) - elif ( - recovery_status == "rerererestoring-clearance-rebuild-reentry" - and current_freshness == "fresh" - and current_momentum == "sustained-clearance" - and current_stability == "stable" - and aligned_fresh_runs_after_reset >= 2 - ): - rerererestore_status = "rerererestored-clearance-rebuild-reentry" - rerererestore_reason = ( - "Fresh clearance-side pressure has re-re-re-restored stronger " - "re-re-restored rebuilt re-entry posture." - ) - elif local_noise and recovery_status in { - "recovering-confirmation-rebuild-reentry-rererestore-reset", - "rerererestoring-confirmation-rebuild-reentry", - "blocked", - }: - rerererestore_status = "blocked" - rerererestore_reason = ( - "Local target instability is still preventing positive confirmation-side " - "re-re-restored rebuilt re-entry re-re-re-restore." - ) - elif recovery_status in { - "recovering-confirmation-rebuild-reentry-rererestore-reset", - "rerererestoring-confirmation-rebuild-reentry", - }: - rerererestore_status = "pending-confirmation-rebuild-reentry-rerererestore" - rerererestore_reason = ( - "Fresh confirmation-side evidence is returning after re-re-restored " - "rebuilt re-entry softened or reset, but it has not yet re-re-re-restored " - "stronger re-re-restored posture." - ) - elif recovery_status in { - "recovering-clearance-rebuild-reentry-rererestore-reset", - "rerererestoring-clearance-rebuild-reentry", - }: - rerererestore_status = "pending-clearance-rebuild-reentry-rerererestore" - rerererestore_reason = ( - "Fresh clearance-side evidence is returning after re-re-restored rebuilt " - "re-entry softened or reset, but it has not yet re-re-re-restored " - "stronger re-re-restored posture." - ) - else: - rerererestore_status = "none" - rerererestore_reason = "" - - return { - "closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_refresh_recovery_score": recovery_score, - "closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_refresh_recovery_status": recovery_status, - "closure_forecast_reset_reentry_rebuild_reentry_restore_rerererestore_status": rerererestore_status, - "closure_forecast_reset_reentry_rebuild_reentry_restore_rerererestore_reason": rerererestore_reason, - "recent_reset_reentry_rebuild_reentry_restore_rererestore_refresh_path": " -> ".join( - closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_refresh_path_label( - event - ) - for event in matching_events - if event - ), - "recent_rererestore_reset_side": recent_rererestore_reset_side, - "aligned_fresh_runs_after_latest_rererestore_reset": aligned_fresh_runs_after_reset, - } def apply_reset_reentry_rebuild_reentry_restore_rererestore_refresh_rerererestore_control(