Persist all v2 state. Call at proxy shutdown.
()
| 90 | |
| 91 | |
| 92 | def on_shutdown() -> None: |
| 93 | """Persist all v2 state. Call at proxy shutdown.""" |
| 94 | if _state_dir is None or not _initialized: |
| 95 | return |
| 96 | |
| 97 | if _index_manager: |
| 98 | try: |
| 99 | _index_manager.save() |
| 100 | logger.info("v2 embedding index saved (%d entries)", _index_manager.size) |
| 101 | except Exception as e: |
| 102 | logger.warning("v2 embedding index save failed: %s", e) |
| 103 | |
| 104 | state = LearnedState( |
| 105 | signal_weights=_weight_tracker.weights if _weight_tracker else [0.55, 0.45], |
| 106 | calibration_temperature=1.0, |
| 107 | shadow_consecutive_wins=_shadow.consecutive_wins if _shadow else 0, |
| 108 | shadow_promoted=_shadow.promoted if _shadow else False, |
| 109 | embedding_index_size=_index_manager.size if _index_manager else 0, |
| 110 | model_priors={}, |
| 111 | ) |
| 112 | save_state(state, _state_dir) |
| 113 | logger.info("v2 lifecycle shutdown: state saved to %s", _state_dir) |
| 114 | |
| 115 | # Complete any pending telemetry records with "unknown" outcome |
| 116 | for rid, rec in list(_pending_telemetry.items()): |
| 117 | try: |
| 118 | from uncommon_route import telemetry as _telem |
| 119 | rec.outcome = "session_end" |
| 120 | _telem.buffer_record(rec) |
| 121 | except Exception: |
| 122 | pass |
| 123 | _pending_telemetry.clear() |
| 124 | |
| 125 | # Flush telemetry buffer on shutdown |
| 126 | try: |
| 127 | from uncommon_route import telemetry as _telem |
| 128 | if _telem.is_enabled(): |
| 129 | count = _telem.flush() |
| 130 | if count: |
| 131 | logger.info("v2 telemetry: flushed %d records on shutdown", count) |
| 132 | except Exception: |
| 133 | pass |
| 134 | |
| 135 | if _metrics: |
| 136 | snap = _metrics.snapshot() |
| 137 | logger.info("v2 session metrics: %s", snap) |
| 138 | |
| 139 | |
| 140 | def associate_request_id(request_id: str) -> None: |
nothing calls this directly
no test coverage detected