From f4fe6cd5e890d1eefba3d568b3333a2804a34b01 Mon Sep 17 00:00:00 2001 From: ARSH871-bot Date: Wed, 11 Nov 2026 09:37:52 +1300 Subject: [PATCH 1/3] Pin that a validator bug is not relabelled as a bad file (fails today) load_policy_file() and load_business_context_file() wrap the validator in the same try as json.loads, so a plain ValueError raised by a bug in the validator is turned into "holds a value that cannot be read", blaming the user's file for our mistake (Shubham's note on #446). These two tests monkeypatch the validator to raise a plain ValueError and assert it is not turned into a PolicyError / BusinessContextError. Both fail on main for the right reason; the next commit makes them pass. --- tests/test_business_context.py | 23 +++++++++++++++++++++++ tests/test_policy_loader.py | 25 +++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/tests/test_business_context.py b/tests/test_business_context.py index 3635fd8..7ee2b2a 100644 --- a/tests/test_business_context.py +++ b/tests/test_business_context.py @@ -30,6 +30,7 @@ import pytest +import analysis.business_context as context_module from analysis.business_context import ( TIERS, BusinessContext, @@ -406,6 +407,28 @@ def test_a_file_json_cannot_hold_faithfully_is_refused_not_crashed_or_trimmed( load_business_context_file(path) +def test_a_bug_in_the_validator_is_not_relabelled_as_a_bad_file(tmp_path, monkeypatch): + """Mirrors the policy loader's test (#446 follow-up). Only json.loads + raises a bare ValueError that is really about the file, so only json.loads + is guarded by `except ValueError`. A bug in load_business_context() must + surface as itself, not as "holds a value that cannot be read".""" + + def broken_validator(parsed): + raise ValueError("a bug in the validator, not in the file") + + monkeypatch.setattr(context_module, "load_business_context", broken_validator) + path = tmp_path / "business-context.json" + path.write_text("[]", encoding="utf-8") + + with pytest.raises(ValueError) as raised: + load_business_context_file(path) + + assert not isinstance(raised.value, BusinessContextError), ( + "a validator bug was turned into a refusal that blames the user's file" + ) + assert "a bug in the validator" in str(raised.value) + + def test_a_real_file_round_trips(tmp_path): path = tmp_path / "business-context.json" path.write_text( diff --git a/tests/test_policy_loader.py b/tests/test_policy_loader.py index 985189e..6ec6106 100644 --- a/tests/test_policy_loader.py +++ b/tests/test_policy_loader.py @@ -26,6 +26,7 @@ import pytest +import analysis.policy as policy_module from analysis.policy import (POLICY_SECTIONS, Policy, PolicyError, load_policy, load_policy_file, _ALLOWED_VALUES, _LEGACY_KEYS, _SECTION_REQUIRED) @@ -242,6 +243,30 @@ def test_a_file_json_cannot_hold_faithfully_is_refused_not_crashed_or_trimmed( load_policy_file(path) +def test_a_bug_in_the_validator_is_not_relabelled_as_a_bad_file(tmp_path, monkeypatch): + """The broad `except ValueError` used to wrap load_policy() too, so a plain + ValueError raised by a validation bug reached the user as "holds a value + that cannot be read", a refusal that blames their file for our mistake + (Shubham's note on #446). Only json.loads raises a bare ValueError that + really is about the file (an integer past 4,300 digits), so only + json.loads is guarded by it. A bug must stay a bug.""" + + def broken_validator(parsed): + raise ValueError("a bug in the validator, not in the file") + + monkeypatch.setattr(policy_module, "load_policy", broken_validator) + path = tmp_path / "policy.json" + path.write_text('{"policy_compliance": []}', encoding="utf-8") + + with pytest.raises(ValueError) as raised: + load_policy_file(path) + + assert not isinstance(raised.value, PolicyError), ( + "a validator bug was turned into a refusal that blames the user's file" + ) + assert "a bug in the validator" in str(raised.value) + + # --------------------------------------------------------------------------- # D2 -- per entry, with an optional top-level default # --------------------------------------------------------------------------- From 21451e5679b26ec91c26b81def7df8195b6e8abc Mon Sep 17 00:00:00 2001 From: ARSH871-bot Date: Wed, 11 Nov 2026 09:38:13 +1300 Subject: [PATCH 2/3] Guard only json.loads with the bare ValueError catch, in both loaders The broad `except ValueError` in load_policy_file() and load_business_context_file() wrapped the validator as well as json.loads. A plain ValueError from a validation bug therefore reached the user as "holds a value that cannot be read", blaming their file for our mistake (Shubham's note on #446). The bare ValueError that really is about the file is json.loads's (an integer past 4,300 digits), so only json.loads is guarded by it. The validators raise PolicyError / BusinessContextError for every problem with a file; load_policy() keeps a RecursionError guard of its own, because a deeply nested rule list does hit it, while load_business_context() does not recurse and gets none. Anything else a validator raises now surfaces as the bug it is. --- analysis/business_context.py | 11 +++++++++-- analysis/policy.py | 17 ++++++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/analysis/business_context.py b/analysis/business_context.py index 30fb48a..197829d 100644 --- a/analysis/business_context.py +++ b/analysis/business_context.py @@ -345,16 +345,21 @@ def load_business_context_file(path: Any) -> BusinessContext: # An empty FILE is an empty CONTEXT, which is valid. return BusinessContext() - # Same three escapes, and the same fix, as policy.load_policy_file(). + # Same three escapes, and the same fix, as policy.load_policy_file(): only + # json.loads is guarded by `except ValueError` and RecursionError, because + # they are about the file only there. load_business_context() raises + # BusinessContextError for every problem with a file and does not recurse + # into nested values, so a bare ValueError from it is a bug and is left + # to show as one (#446 follow-up, Shubham's note). try: parsed = json.loads(text, object_pairs_hook=_refuse_duplicate_keys) - return load_business_context(parsed) except json.JSONDecodeError as error: raise BusinessContextError( f"{file_path} is not valid JSON: line {error.lineno}, " f"column {error.colno}: {error.msg}" ) from error except BusinessContextError: + # _refuse_duplicate_keys() runs inside json.loads and raises this. raise except RecursionError as error: raise BusinessContextError( @@ -365,6 +370,8 @@ def load_business_context_file(path: Any) -> BusinessContext: f"{file_path} holds a value that cannot be read: {error}" ) from error + return load_business_context(parsed) + def _refuse_duplicate_keys(pairs: List[Tuple[str, Any]]) -> Dict[str, Any]: """Build one JSON object, refusing a key that appears twice in it. diff --git a/analysis/policy.py b/analysis/policy.py index 6209d4c..64847da 100644 --- a/analysis/policy.py +++ b/analysis/policy.py @@ -714,21 +714,36 @@ def load_policy_file(path: Any) -> Policy: # on upload rather than a refusal naming the file: an integer of more # than 4,300 digits (ValueError), and nesting deeper than Python's # recursion limit, hit by json.loads or by load_policy() walking it. + # + # THE TWO STEPS ARE GUARDED SEPARATELY (#446 follow-up, Shubham's note). + # The bare ValueError is json.loads's, and only json.loads's: it is the + # one that really is about the file. Wrapping load_policy() in the same + # `except ValueError` made a plain ValueError from a validation BUG + # reach the user as "holds a value that cannot be read", a refusal that + # blames their file for our mistake. load_policy() raises PolicyError + # for every problem with a file, so it is guarded only for the + # RecursionError a deeply nested rule list causes, and anything else it + # raises stays visible as the bug it is. try: parsed = json.loads(text, object_pairs_hook=_refuse_duplicate_keys) - return load_policy(parsed) except json.JSONDecodeError as error: raise PolicyError( f"{file_path} is not valid JSON: line {error.lineno}, " f"column {error.colno}: {error.msg}" ) from error except PolicyError: + # _refuse_duplicate_keys() runs inside json.loads and raises this. raise except RecursionError as error: raise PolicyError(f"{file_path} is nested too deeply to be a policy") from error except ValueError as error: raise PolicyError(f"{file_path} holds a value that cannot be read: {error}") from error + try: + return load_policy(parsed) + except RecursionError as error: + raise PolicyError(f"{file_path} is nested too deeply to be a policy") from error + def _refuse_duplicate_keys(pairs: List[Tuple[str, Any]]) -> Dict[str, Any]: """Build one JSON object, refusing a key that appears twice in it. From 508746668a91293686b3b69f31343be093d61a88 Mon Sep 17 00:00:00 2001 From: ARSH871-bot Date: Wed, 11 Nov 2026 09:39:18 +1300 Subject: [PATCH 3/3] Pin that a duplicate-key refusal is not re-wrapped as a generic one Found by mutating my own change: with 'except PolicyError: raise' removed from the json.loads block, the duplicate-key refusal was re-wrapped as 'holds a value that cannot be read: ... appears twice', and every test still passed, because the parametrized test matches on a substring. That guard's one job is now this, so it needs a test that can tell. --- tests/test_business_context.py | 15 +++++++++++++++ tests/test_policy_loader.py | 17 +++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/tests/test_business_context.py b/tests/test_business_context.py index 7ee2b2a..11fcc1b 100644 --- a/tests/test_business_context.py +++ b/tests/test_business_context.py @@ -407,6 +407,21 @@ def test_a_file_json_cannot_hold_faithfully_is_refused_not_crashed_or_trimmed( load_business_context_file(path) +def test_a_duplicate_key_refusal_keeps_its_own_message_not_a_generic_one(tmp_path): + """Mirrors the policy loader's test: BusinessContextError is a ValueError, + so without `except BusinessContextError: raise` the duplicate-key refusal + raised inside json.loads would be re-wrapped as "holds a value that cannot + be read", which a substring match on "appears twice" still accepts.""" + path = tmp_path / "business-context.json" + path.write_bytes(b'[{"device": "rtr-us5", "tier": "critical", "tier": "standard"}]') + + with pytest.raises(BusinessContextError) as raised: + load_business_context_file(path) + + assert "appears twice" in str(raised.value) + assert "cannot be read" not in str(raised.value) + + def test_a_bug_in_the_validator_is_not_relabelled_as_a_bad_file(tmp_path, monkeypatch): """Mirrors the policy loader's test (#446 follow-up). Only json.loads raises a bare ValueError that is really about the file, so only json.loads diff --git a/tests/test_policy_loader.py b/tests/test_policy_loader.py index 6ec6106..29a4750 100644 --- a/tests/test_policy_loader.py +++ b/tests/test_policy_loader.py @@ -243,6 +243,23 @@ def test_a_file_json_cannot_hold_faithfully_is_refused_not_crashed_or_trimmed( load_policy_file(path) +def test_a_duplicate_key_refusal_keeps_its_own_message_not_a_generic_one(tmp_path): + """_refuse_duplicate_keys() raises a PolicyError from inside json.loads, + and PolicyError is a ValueError. Without `except PolicyError: raise` the + bare `except ValueError` beside it re-wraps it as "holds a value that + cannot be read: ... appears twice", which a substring match on "appears + twice" still accepts, so the parametrized test above cannot tell. This + one can: the user is told what is actually wrong, in the loader's words.""" + path = tmp_path / "policy.json" + path.write_bytes(b'{"policy_compliance": [' + _A_RULE + b'], "policy_compliance": []}') + + with pytest.raises(PolicyError) as raised: + load_policy_file(path) + + assert "appears twice" in str(raised.value) + assert "cannot be read" not in str(raised.value) + + def test_a_bug_in_the_validator_is_not_relabelled_as_a_bad_file(tmp_path, monkeypatch): """The broad `except ValueError` used to wrap load_policy() too, so a plain ValueError raised by a validation bug reached the user as "holds a value