From 225b9b30f1fbd6fb85afe26d1cc012578a703d72 Mon Sep 17 00:00:00 2001 From: Joey Yakimowich-Payne Date: Sun, 15 Mar 2026 08:27:05 -0600 Subject: [PATCH] fix: scale FM window in _apply_fidelity to match real API pressure _apply_fidelity checked fm.current_pressure() which uses internal object tokens (tiny) / 200k window = always NORMAL. Now scales window_size using last_effective token count so FM pressure matches real context usage, enabling L0->L1->L2 degradations. --- src/mnemosyne/gateway.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/mnemosyne/gateway.py b/src/mnemosyne/gateway.py index 1758fa5..047c1f7 100644 --- a/src/mnemosyne/gateway.py +++ b/src/mnemosyne/gateway.py @@ -1414,8 +1414,22 @@ def _apply_fidelity(payload: dict, session: "Session") -> None: obj_id = fm.register_object(obj) content_map[key] = obj_id - # Check pressure and log - pressure = fm.current_pressure() + # Check pressure and log. + # Scale window_size so FM's internal pressure matches real API pressure. + # (Same fix as _update_fidelity_pressure — FM's total_tokens() is only + # a fraction of the real context, so internal pressure is always NORMAL.) + obj_tokens = fm.total_tokens() + if obj_tokens > 0 and fm.window_size > 0: + real_pressure_ratio = session.token_state.get("last_effective", 0) / fm.window_size + if real_pressure_ratio > 0: + saved_ws = fm.window_size + fm.window_size = max(1, int(obj_tokens / real_pressure_ratio)) + pressure = fm.current_pressure() + fm.window_size = saved_ws + else: + pressure = fm.current_pressure() + else: + pressure = fm.current_pressure() if pressure > PressureZone.NORMAL: transitions = fm.degrade(turn) for obj_id, old_level, new_level in transitions: