From a8eb397ebd819be77c3ec6c9e91483d69512d4ab Mon Sep 17 00:00:00 2001 From: Chris Busillo Date: Sun, 31 May 2026 17:17:21 -0400 Subject: [PATCH] Fix size followup intent gates --- .../web/runtime/folder_tuning_advice.py | 2 + .../web/runtime/folder_tuning_helpers.py | 11 +++- tests/test_tuning_runtime.py | 55 +++++++++++++++++++ 3 files changed, 65 insertions(+), 3 deletions(-) diff --git a/mediaforce/web/runtime/folder_tuning_advice.py b/mediaforce/web/runtime/folder_tuning_advice.py index 15250aa..99822af 100644 --- a/mediaforce/web/runtime/folder_tuning_advice.py +++ b/mediaforce/web/runtime/folder_tuning_advice.py @@ -290,6 +290,8 @@ def _merge_operator_note_parse( merged["operator_confirmed"] = True if str(merged.get("intent_type") or "").strip().lower() in {"unclear", "other"}: merged["intent_type"] = "direct_request" + if heuristic.get("measured_size_followup"): + merged["measured_size_followup"] = True merged = _normalize_operator_note_parse(merged) if merged is None: return heuristic diff --git a/mediaforce/web/runtime/folder_tuning_helpers.py b/mediaforce/web/runtime/folder_tuning_helpers.py index c0e4839..1d46144 100644 --- a/mediaforce/web/runtime/folder_tuning_helpers.py +++ b/mediaforce/web/runtime/folder_tuning_helpers.py @@ -116,7 +116,10 @@ def measured_size_budget_policy_fragment( if str(request.get("request_type") or "").strip().lower() not in {"size_budget", "combined_experiment"}: return {} size_budget_request = object_dict(request.get("size_budget_request")) - hard_size_cap = bool(request.get("hard_size_cap") or size_budget_request.get("hard_size_cap")) + hard_size_cap = bool( + request.get("operator_confirmed") + and (request.get("hard_size_cap") or size_budget_request.get("hard_size_cap")) + ) measured_size_followup = bool( request.get("operator_confirmed") and (request.get("measured_size_followup") or size_budget_request.get("measured_size_followup")) @@ -151,8 +154,10 @@ def allows_measured_size_quality_tradeoff( return False size_budget_request = object_dict(request.get("size_budget_request")) return bool( - request.get("hard_size_cap") - or size_budget_request.get("hard_size_cap") + ( + request.get("operator_confirmed") + and (request.get("hard_size_cap") or size_budget_request.get("hard_size_cap")) + ) or ( request.get("operator_confirmed") and (request.get("measured_size_followup") or size_budget_request.get("measured_size_followup")) diff --git a/tests/test_tuning_runtime.py b/tests/test_tuning_runtime.py index ac433a1..fd466d6 100644 --- a/tests/test_tuning_runtime.py +++ b/tests/test_tuning_runtime.py @@ -3555,6 +3555,47 @@ def test_operator_requested_experiment_marks_measured_size_followup(self) -> Non self.assertTrue(request["measured_size_followup"]) self.assertFalse(request["hard_size_cap"]) + def test_operator_requested_experiment_keeps_heuristic_followup_when_structured_parse_misses_it(self) -> None: + structured_parse = { + "summary": "Size follow-up request.", + "intent_type": "direct_request", + "request_type": "size_budget", + "operator_confirmed": True, + "measured_size_followup": False, + "metric": None, + "metric_target": None, + "size_budget_value": 300, + "size_budget_unit": "mb", + "scale_height": None, + "black_bar_handling": None, + "crop": None, + "hard_size_cap": False, + "reasoning_note": "Structured parse missed the measured follow-up wording.", + } + + with patch( + "mediaforce.web.runtime.folder_tuning_advice.request_operator_note_parse", + return_value=structured_parse, + ): + request = _operator_requested_experiment( + "Revise this sample smaller toward 300 MB per episode. The last sample was 766 MiB, 2.6x target.", + { + "source_size_bytes": 4_349_049_136, + "duration_seconds": 3161.376, + "audio_summary": [{"codec_name": "ac3", "channels": 6}], + "resolved_policy": { + "video": {"encoder": "libsvtav1"}, + "audio": { + "convert_to_opus_codecs": ["ac3"], + "surround_5_1_opus_bitrate": "256k", + }, + }, + }, + ) + + assert request is not None + self.assertTrue(request["measured_size_followup"]) + def test_operator_requested_experiment_detects_scale_target_request(self) -> None: request = _operator_requested_experiment("Downsample the 4K files to 1080p for this folder.") @@ -4514,11 +4555,25 @@ def test_measured_size_budget_policy_fragment_ignores_unconfirmed_followup_after self.assertEqual(fragment, {}) + def test_measured_size_budget_policy_fragment_ignores_unconfirmed_hard_cap_after_miss(self) -> None: + fragment = measured_size_budget_policy_fragment( + operator_request={ + "request_type": "size_budget", + "requested_max_encoded_percent": 7.23, + "operator_confirmed": False, + "hard_size_cap": True, + }, + size_target_analysis={"status": "over_target"}, + ) + + self.assertEqual(fragment, {}) + def test_measured_size_budget_policy_fragment_enforces_explicit_hard_cap_after_miss(self) -> None: fragment = measured_size_budget_policy_fragment( operator_request={ "request_type": "size_budget", "requested_max_encoded_percent": 7.23, + "operator_confirmed": True, "hard_size_cap": True, }, size_target_analysis={"status": "over_target"},