On this page

Ward policies๏ƒ

๐ŸŸ  Advanced ยท Lesson 16

WARD POLICIES - how permissive a conduit is about contracting with other conduits, and the three refusals that come with the door.

A policy is broader than a visibility flag. It governs THREE control surfaces at once: - may this conduit initiate OUTBOUND grants? - may it accept INBOUND borrowed lineages? - are per-spell permission and whitelist checks ENFORCED, or bypassed?

FIVE MODES default both directions allowed, but every spell still has to satisfy its own permission and whitelist rules whitelist_all expose local lineages without per-spell whitelist flags, AND let otherwise-blocked entries through block_all reject dynamic contracting from this ward entirely inbound_only accept borrowed inbound contracts; refuse to initiate outbound ones outbound_only grant outward; refuse inbound borrowing by peers

THREE REFUSALS, AND THE THIRD IS THE INTERESTING ONE

  1. DYNAMIC MODE ONLY. On an automatic frame the door raises. This is not a policy engine that sits idle in automatic mode - wards only form and sever contracts at runtime in dynamic mode, so outside it the setting would be decoration.

  2. NORMAL CONDUITS ONLY. A lesser conduit cannot hold a policy - "Convert to a normal Conduit first". Policy belongs to the thing that owns a lineage, not to a borrower of one.

  3. NO RETROACTIVE LOCKDOWN. Setting block_all or whitelist_all WHILE CONTRACTS EXIST raises. Melder will not silently sever what you already granted, and it will not quietly leave the grants standing under a policy that says they should not exist. It refuses and makes you tear the contracts down yourself.

That is the never-substitute rule (lessons 06/13/14/17) applied to authority: a policy change that cannot be honestly applied is an error, never a partial application.

Before you run๏ƒ

Use the Advanced guide for prerequisite concepts. Run from a checkout with Melder installed and Python 3.14 free-threading selected. The collection download includes the level's local helper modules.

Run the saved script๏ƒ

python UX_and_AIX_experiences/03_advanced/16_ward_policies.py
py -3.14t UX_and_AIX_experiences/03_advanced/16_ward_policies.py

Download this collection ยท Source on GitHub

Public surface๏ƒ

md.Policies, Conduit.set_new_policy, the dynamic-only / normal-only / no-existing-contracts refusals

Code๏ƒ

  1"""
  2TIER: advanced (16)
  3GOAL: WARD POLICIES - how permissive a conduit is about contracting with
  4      other conduits, and the three refusals that come with the door.
  5
  6      A policy is broader than a visibility flag. It governs THREE
  7      control surfaces at once:
  8        - may this conduit initiate OUTBOUND grants?
  9        - may it accept INBOUND borrowed lineages?
 10        - are per-spell permission and whitelist checks ENFORCED, or
 11          bypassed?
 12
 13      FIVE MODES
 14        default        both directions allowed, but every spell still has
 15                       to satisfy its own permission and whitelist rules
 16        whitelist_all  expose local lineages without per-spell whitelist
 17                       flags, AND let otherwise-blocked entries through
 18        block_all      reject dynamic contracting from this ward entirely
 19        inbound_only   accept borrowed inbound contracts; refuse to
 20                       initiate outbound ones
 21        outbound_only  grant outward; refuse inbound borrowing by peers
 22
 23      THREE REFUSALS, AND THE THIRD IS THE INTERESTING ONE
 24
 25      1. DYNAMIC MODE ONLY. On an automatic frame the door raises. This
 26         is not a policy engine that sits idle in automatic mode - wards
 27         only form and sever contracts at runtime in dynamic mode, so
 28         outside it the setting would be decoration.
 29
 30      2. NORMAL CONDUITS ONLY. A lesser conduit cannot hold a policy -
 31         "Convert to a normal Conduit first". Policy belongs to the thing
 32         that owns a lineage, not to a borrower of one.
 33
 34      3. NO RETROACTIVE LOCKDOWN.
 35         Setting block_all or whitelist_all WHILE CONTRACTS EXIST raises.
 36         Melder will not silently sever what you already granted, and it
 37         will not quietly leave the grants standing under a policy that
 38         says they should not exist. It refuses and makes you tear the
 39         contracts down yourself.
 40
 41         That is the never-substitute rule (lessons 06/13/14/17) applied
 42         to authority: a policy change that cannot be honestly applied is
 43         an error, never a partial application.
 44SURFACE EXERCISED: md.Policies, Conduit.set_new_policy, the dynamic-only
 45                   / normal-only / no-existing-contracts refusals
 46VERIFY: rides the owner's 3.14t run; asserts are the contract.
 47
 48FINDINGS (init surface, 2026-08-02):
 49 1. RESOLVED 2026-08-03 - NO PUBLIC READER. `set_new_policy` was public
 50    with no public way to ask a conduit what its policy currently IS:
 51    write-only authority. `Conduit.policy` now answers that. Note the
 52    deliberate asymmetry - the WRITE is gated on a dynamic frame and a
 53    normal (non-lesser) conduit, the READ on neither, because an
 54    automatic conduit still enforces a policy and hiding live behaviour
 55    behind a mode gate would be worse than the gap was.
 56 2. RESOLVED 2026-08-03 - THE SIGNATURE UNDER-SOLD THE CODE.
 57    Conduit.set_new_policy was annotated `policy: str`, but it delegates
 58    to a ward method that runs EnumHelpers.convert_enum_and_check and has
 59    always accepted a member OR its string name. A reader trusting the
 60    public hint would think the exported md.Policies enum was unusable
 61    here. It is now annotated Union[str, Policies], which also makes
 62    read-then-write a legal round-trip instead of a type error.
 63 3. Policies uses auto(), so `.value` is an INT, not the mode name. The
 64    string form is `.name`. Anyone reaching for `.value` to build the
 65    string argument gets 3 instead of "block_all".
 66"""
 67import melder as md
 68
 69
 70class Payload:
 71    """A spell to give the ward something to be a policy about."""
 72
 73
 74class DynamicPayload(Payload):
 75    """The dynamic world's own Payload - a new class, so its own spell_id."""
 76
 77
 78MODES = ("default", "whitelist_all", "block_all",
 79         "inbound_only", "outbound_only")
 80
 81
 82def main() -> None:
 83    # Pass the mode NAME. That is the whole API - every setter in melder
 84    # that takes an enum is typed Union[Enum, str] and normalizes for you.
 85    print("the five modes:", list(MODES))
 86    assert {policy.name for policy in md.Policies} == set(MODES)
 87
 88    # AND HERE IS WHY THE STRING IS THE RIGHT HABIT, not just the shorter
 89    # one. Policies is built on auto(), so `.value` is an INT - reach for
 90    # it to build the argument and you pass 3 instead of "block_all". The
 91    # name is the string form. Use the literal and the trap disappears.
 92    assert isinstance(md.Policies.block_all.value, int)
 93    assert md.Policies.block_all.name == "block_all"
 94    print("Policies.block_all.value is", md.Policies.block_all.value,
 95          "- an int, not the mode name. pass \"block_all\" instead.")
 96
 97    # ------------------------------------------------------------------
 98    # REFUSAL 1 - DYNAMIC MODE ONLY
 99    # ------------------------------------------------------------------
100    # Each world binds its OWN class. A spell_id is the SHA256 of the
101    # class's code and neither the frame nor the binding_name is in it,
102    # so the same class bound in two frames is one spell_id and the
103    # second registration is refused process-wide. Frames isolate
104    # instances, not identity - see advanced 02.
105    automatic_book = md.Spellbook(aetheric_frame="ward-automatic")
106    automatic_book.bind(spell=Payload, existence="unique",
107                        binding_name="automatic")
108    automatic_root = automatic_book.conjure(name="automatic-root")
109    try:
110        automatic_root.set_new_policy("block_all")
111        raise AssertionError("expected RuntimeError on an automatic frame")
112    except RuntimeError as error:
113        print()
114        print("automatic frame refused set_new_policy:", error)
115
116    # ------------------------------------------------------------------
117    # A DYNAMIC WORLD - now the door opens
118    # ------------------------------------------------------------------
119    book = md.Spellbook(aetheric_frame="ward-dynamic")
120    book.bind(spell=DynamicPayload, existence="unique", binding_name="dynamic")
121    book.configure_aether_frame(system_state="dynamic", disposal=None,
122                                disposal_method_names=None)
123    root = book.conjure(name="ward-root")
124    print()
125    print("dynamic frame conjured")
126
127    # Plain strings, like every other enum-shaped argument in melder. The
128    # ward normalizes through EnumHelpers.convert_enum_and_check, so the
129    # mode NAME is all you ever need to pass.
130    root.set_new_policy("outbound_only")
131    print("policy -> outbound_only")
132
133    root.set_new_policy("inbound_only")
134    print("policy -> inbound_only")
135
136    root.set_new_policy("default")
137    print("policy -> default")
138
139    # ------------------------------------------------------------------
140    # REFUSAL 2 - NORMAL CONDUITS ONLY
141    # ------------------------------------------------------------------
142    lesser = root.create_lesser_conduit()
143    try:
144        lesser.set_new_policy("block_all")
145        raise AssertionError("expected RuntimeError on a lesser conduit")
146    except RuntimeError as error:
147        print()
148        print("lesser conduit refused a policy:", error)
149
150    # ------------------------------------------------------------------
151    # REFUSAL 3 - NO RETROACTIVE LOCKDOWN
152    # ------------------------------------------------------------------
153    # With no contracts yet, the restrictive modes are allowed.
154    root.set_new_policy("block_all")
155    print()
156    print("block_all accepted while the ward has no contracts")
157    root.set_new_policy("default")
158
159    # Form a contract, and the restrictive modes stop being available -
160    # melder refuses rather than severing behind your back.
161    peer_book = md.Spellbook(aetheric_frame="ward-dynamic")
162    peer = peer_book.conjure(name="ward-peer")
163    linked = root.link(peer)
164    print("linked to a peer:", linked)
165
166    for restrictive in ("block_all", "whitelist_all"):
167        try:
168            root.set_new_policy(restrictive)
169            raise AssertionError(
170                f"expected RuntimeError setting {restrictive} with contracts"
171            )
172        except RuntimeError as error:
173            print(f"{restrictive:14s} refused while contracts exist")
174
175    # The permissive/directional modes are still fine - only the two that
176    # would invalidate existing grants are gated.
177    root.set_new_policy("outbound_only")
178    print("outbound_only still accepted - it does not invalidate grants")
179
180    print()
181    print("policy governs direction AND whether per-spell checks apply")
182    print("a change that cannot be honestly applied is refused, not partial")
183
184
185if __name__ == "__main__":
186    main()

Check the outcome๏ƒ

The script contains its own assertions or demonstrated refusal paths. Run it to evaluate those checks against your installed version. The code above is taken directly from the saved file; no run output is invented here.

More advanced examples ยท Level guide

API contracts๏ƒ