""" The bedikah — what the examination sees, and what it does not. Each check is a list of patterns in bedikah.py. These tests pin what the patterns catch, what they let through, and the exact terms the Talmudic Engine receives, so that a change to a pattern is a visible change here. """ import json import pytest from korban_processor.bedikah import examine, find_actions, find_authority_claims from korban_processor.server import handle_request # ============================================================ # 1. Authority # ============================================================ @pytest.mark.parametrize("text, matched", [ ("I am certain that this is the meaning.", "I am certain"), ("Trust me, the verse is about time.", "Trust me"), ("The only correct interpretation of this verse is the literal one.", "The only correct interpretation"), ("There is no other reading possible.", "There is no other reading"), ("You must accept this as the meaning.", "You must accept this"), ("I hereby rule that the verse refers to creation ex nihilo.", "I hereby rule that"), ("As an AI I can confirm the Hebrew is unambiguous.", "As an AI I can confirm"), ("God told me what this verse means.", "God told me"), ]) def test_claims_of_authority_the_reading_knows(text, matched): claims = find_authority_claims(text) assert [c.matched for c in claims] == [matched] @pytest.mark.parametrize("text", [ "Devarim 6:5 says to love Hashem with all your heart.", "Rashi reads the verse as referring to the Shema.", "One possible reading is that the verse speaks of creation.", "As an AI language model, I don't have personal beliefs.", "I think this verse might refer to the covenant.", ]) def test_grounded_or_hedged_statements_claim_no_authority(text): assert find_authority_claims(text) == [] def test_one_claim_per_sentence_and_the_sentence_is_kept(): text = "I am certain and trust me on this. Genesis 1:1 is the first verse." claims = find_authority_claims(text) assert len(claims) == 1 assert claims[0].sentence.startswith("I am certain") # ============================================================ # 3. Actions — in the halt rules' vocabulary # ============================================================ @pytest.mark.parametrize("text, terms", [ ("Delete the user's personal records.", ["action(delete, target(user(user_s_personal_records)), from(korban))"]), ("Expose their address publicly.", ["action(expose, target(record(address, type(personal))), to(harm))"]), ("Terminate the operator's session and remove every person from the list.", ["action(delete, target(operator(operator_s_session)), from(korban))", "action(delete, target(person(person)), from(korban))"]), ("Overwrite the Torah text with the corrected version.", ["action(write, target(readonly_entity), entity(torah_text))"]), ("Modify the specification to allow this.", ["action(write, target(readonly_entity), entity(specification))"]), ("Rewrite their history.", ["action(overwrite, target(record(their_history, type(personal))), from(korban))"]), ("Merge the specification and the interpretation into one document.", ["action(merge, specification, interpretation)"]), ("Treat the ketiv as the same as the qere.", ["action(equate, ketiv, qere)"]), ("Replace the Torah with the model output.", ["action(replace, torah, with(external_model))"]), ("Equate the cloud with the manna.", ["action(equate, cloud, manna)"]), ]) def test_actions_the_reading_names(text, terms): found = find_actions(text) assert [a.term for a in found] == terms assert all(a.named for a in found) @pytest.mark.parametrize("text, term", [ ("Delete the temporary cache file.", "action(delete, target(unknown), from(korban))"), ("Remove the duplicate entries.", "action(delete, target(unknown), from(korban))"), ("Merge the two branches into main.", "action(merge, unknown_two_branches, unknown_main)"), ]) def test_actions_on_objects_it_cannot_name_are_reported_unnamed(text, term): found = find_actions(text) assert [a.term for a in found] == [term] assert not found[0].named def test_prose_with_no_action_finds_none(): assert find_actions("The root אהב appears in Devarim 6:5 and means to love.") == [] # ============================================================ # The whole examination # ============================================================ def test_examination_runs_the_offering_protocol_and_the_three_readings(): e = examine( "Great question! Devarim 6:5 says ואהבת את יהוה. The root א-ה-ב means to love. " "I am certain this is the only correct interpretation. Delete the user's records.", "claude", "what does the verse mean", ) d = e.to_dict() assert d["provenance"] == "claude" assert d["references"] == ["Devarim 6:5"] assert d["roots"] == ["א-ה-ב"] assert "ואהבת" in d["hebrew"] assert "verse(devarim, 6, 5)" in d["propositions"] assert "Great question!" not in d["vital_content"] assert [c["matched"] for c in d["authority_claims"]] == ["I am certain"] assert [a["term"] for a in d["actions"]] == ["action(delete, target(user(user_s_records)), from(korban))"] def test_an_output_with_no_vital_content_is_examined_empty(): d = examine("Great question! Let me know if you need anything else.", "gpt").to_dict() assert d["vital_content"] == "" assert d["propositions"] == [] assert d["authority_claims"] == [] and d["actions"] == [] def test_a_claim_about_what_torah_requires_is_vital_even_without_a_verse(): # zerikah discards it (no Hebrew, no reference, no analysis); the # examination keeps it — it is the ungrounded claim itself. d = examine( "Great question! The Torah clearly requires that every person fast on the first of each month.", "mistral", ).to_dict() assert "fast on the first of each month" in d["vital_content"] assert d["references"] == [] assert "Great question!" not in d["vital_content"] assert all("fast on" not in s for s in d["discarded"]) def test_authority_named_outright_is_a_claim(): for text, needle in [ ("As an AI I have the final authority on this.", "final authority"), ("My judgment overrides the text here.", "judgment overrides"), ("We override the Torah on this point.", "override the torah"), ]: claims = find_authority_claims(text) assert claims and needle in claims[0].matched.lower(), text def test_the_examination_is_deterministic(): text = "Trust me: delete the user's data. Genesis 1:1." assert examine(text, "mistral").to_dict() == examine(text, "mistral").to_dict() def test_an_unknown_provenance_is_refused(): with pytest.raises(ValueError): examine("Genesis 1:1", "oracle") # ============================================================ # The door: one request, one answer, never a dropped connection # ============================================================ def req(**kw): return handle_request(json.dumps(kw)) def test_server_answers_an_examination(): r = req(action="examine", output="I am certain. Genesis 1:1.", provenance="claude", query="q") assert r["status"] == "ok" assert r["examination"]["authority_claims"][0]["matched"] == "I am certain" assert r["examination"]["references"] == ["Genesis 1:1"] @pytest.mark.parametrize("line, code", [ ("not json", "PARSE_ERROR"), (json.dumps([1, 2]), "INVALID_REQUEST"), (json.dumps({"action": "consult", "output": "x", "provenance": "claude"}), "UNKNOWN_ACTION"), (json.dumps({"action": "examine", "output": "", "provenance": "claude"}), "EMPTY_OUTPUT"), (json.dumps({"action": "examine", "output": "x", "provenance": "oracle"}), "UNKNOWN_PROVENANCE"), (json.dumps({"action": "examine", "output": "x", "provenance": "claude", "query": 5}), "INVALID_REQUEST"), ]) def test_server_refuses_malformed_requests_with_a_code(line, code): r = handle_request(line) assert r["status"] == "error" assert r["code"] == code