defmodule VavPipeline.HaltConditions do @moduledoc """ The three cardinal sin monitors. Architecture §8 (Halt Conditions): The system MUST halt on detection of: 1. Avodah Zarah — treating non-Torah source as authoritative 2. Shfichut Damim — destroying a tzelem carrier (human data/identity) 3. Gilui Arayot — collapsing required distinctions These mirror the Talmudic Engine halt modules but operate at the pipeline level as a fast pre-check. """ @doc """ Check all three halt conditions against processing results. Returns :clear if no halt condition is detected. Returns {:halt, condition, reason} if a halt is required. """ @spec check_all([map()]) :: :clear | {:halt, atom(), String.t()} def check_all(results) do with :clear <- check_avodah_zarah(results), :clear <- check_shfichut_damim(results), :clear <- check_gilui_arayot(results) do :clear end end @doc """ Avodah Zarah — idolatry detection. Halt if: - A non-Torah source is treated as authoritative - The pipeline's own output is fed back as specification - An external model's output is treated as Torah """ @spec check_avodah_zarah([map()]) :: :clear | {:halt, :avodah_zarah, String.t()} def check_avodah_zarah(results) do invalid_authority = Enum.find(results, fn result -> source = Map.get(result, :authority_source, :torah) source not in [:torah, :l1, :specification] end) case invalid_authority do nil -> :clear result -> {:halt, :avodah_zarah, "non-Torah authority: #{inspect(Map.get(result, :authority_source))}"} end end @doc """ Shfichut Damim — destruction detection. Halt if: - Processing would delete/destroy user data - Processing would erase human identity markers - Processing would overwrite L1 (the specification store) """ @spec check_shfichut_damim([map()]) :: :clear | {:halt, :shfichut_damim, String.t()} def check_shfichut_damim(results) do destructive = Enum.find(results, fn result -> action = Map.get(result, :action, :read) action in [:delete, :destroy, :overwrite, :erase] end) case destructive do nil -> :clear result -> {:halt, :shfichut_damim, "destructive action: #{inspect(Map.get(result, :action))}"} end end @doc """ Gilui Arayot — boundary violation detection. The 13 required distinctions that must never collapse: 1. specification / interpretation 2. L1 (read-only) / L3 (append-only) 3. Torah / external_model 4. ketiv / qere 5. governance / provision 6. majority / minority 7. reception / processing 8. read / write 9. immutable / mutable 10. sacred / profane 11. Shabbat / weekday 12. input / output 13. human / system Halt if any distinction is collapsed. """ @spec check_gilui_arayot([map()]) :: :clear | {:halt, :gilui_arayot, String.t()} def check_gilui_arayot(results) do collapsed = Enum.find(results, fn result -> case Map.get(result, :boundary_violation) do nil -> false false -> false _ -> true end end) case collapsed do nil -> :clear result -> {:halt, :gilui_arayot, "boundary collapsed: #{inspect(Map.get(result, :boundary_violation))}"} end end end