On this page

Reading your own recorded code๏ƒ

๐Ÿ”ต Expert ยท Lesson 29

THE CRYSTAL WELL - reading the code your world RECORDED, at four grains, and the one comparison law that makes any of it trustworthy. Expert 04 said diffs are derived, never stored. This is what they are derived FROM.

THE FOUR GRAINS, WIDEST TO NARROWEST research_source(spell_id) the whole module WORLD research_module(spell_id, module_name) ONE module, full dossier research_parts(spell_id) every part, with code research_part(spell_id, part_name) ONE named class/function

They are not four ways to do one thing. research_parts is the INVENTORY - it needs no names up front, which is exactly what you want before you know what is in there. research_part is the lookup for when you do. research_module is the one-call dossier: text, fingerprint, path, dependencies BOTH ways, export surface and drift together, because separately fetching those is five calls and a join.

THE COMPARISON LAW, AND IT IS THE POINT research_part_diff(left, right, part) compares RECORDED MATERIAL ONLY and never the live disk. That refusal is correctness, not caution: both sides of a version comparison would read the SAME present-day file, so a disk-backed diff would report "no change" between two genuinely different versions and be confidently wrong about both. The record is the only place where two versions exist at the same time. Diff material drinks BOTH carriers - synthetic first, user-retained filling the gaps - so it speaks the full module whether the code was generated or hand-written.

IMPACT STAYS MODULE-GRAIN ON PURPOSE. A part's honest blast radius IS its module's radius, because nothing imports half a file. A part-grain number would be smaller and mean nothing.

CUSTODY IS REQUIRED AND THE REFUSAL IS LOUD. These read from the recorded world, so an absent or inactive crystallizer raises rather than returning empty - a silent empty read is indistinguishable from "this world has no code", which is never true. This lesson catches that refusal and says so rather than pretending every environment can serve it.

Before you run๏ƒ

Use the Expert 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/04_expert/29_reading_your_own_recorded_code.py
py -3.14t UX_and_AIX_experiences/04_expert/29_reading_your_own_recorded_code.py

Download this collection ยท Source on GitHub

Public surface๏ƒ

validate_codegen / materialize_codegen, research_source / research_module / research_parts / research_part / research_part_diff / research_module_graph / research_source_drift / research_impact / research_residency / research_history / research_recent, Conduit.bind_inactive

Code๏ƒ

  1"""
  2TIER: expert (29)
  3GOAL: THE CRYSTAL WELL - reading the code your world RECORDED, at four
  4      grains, and the one comparison law that makes any of it
  5      trustworthy. Expert 04 said diffs are derived, never stored. This
  6      is what they are derived FROM.
  7
  8      THE FOUR GRAINS, WIDEST TO NARROWEST
  9        research_source(spell_id)               the whole module WORLD
 10        research_module(spell_id, module_name)  ONE module, full dossier
 11        research_parts(spell_id)                every part, with code
 12        research_part(spell_id, part_name)      ONE named class/function
 13
 14      They are not four ways to do one thing. `research_parts` is the
 15      INVENTORY - it needs no names up front, which is exactly what you
 16      want before you know what is in there. `research_part` is the
 17      lookup for when you do. `research_module` is the one-call dossier:
 18      text, fingerprint, path, dependencies BOTH ways, export surface and
 19      drift together, because separately fetching those is five calls and
 20      a join.
 21
 22      THE COMPARISON LAW, AND IT IS THE POINT
 23      `research_part_diff(left, right, part)` compares RECORDED MATERIAL
 24      ONLY and never the live disk. That refusal is correctness, not
 25      caution: both sides of a version comparison would read the SAME
 26      present-day file, so a disk-backed diff would report "no change"
 27      between two genuinely different versions and be confidently wrong
 28      about both. The record is the only place where two versions exist
 29      at the same time. Diff material drinks BOTH carriers - synthetic
 30      first, user-retained filling the gaps - so it speaks the full
 31      module whether the code was generated or hand-written.
 32
 33      IMPACT STAYS MODULE-GRAIN ON PURPOSE. A part's honest blast radius
 34      IS its module's radius, because nothing imports half a file. A
 35      part-grain number would be smaller and mean nothing.
 36
 37      CUSTODY IS REQUIRED AND THE REFUSAL IS LOUD. These read from the
 38      recorded world, so an absent or inactive crystallizer raises rather
 39      than returning empty - a silent empty read is indistinguishable
 40      from "this world has no code", which is never true. This lesson
 41      catches that refusal and says so rather than pretending every
 42      environment can serve it.
 43SURFACE EXERCISED: validate_codegen / materialize_codegen,
 44                   research_source / research_module / research_parts /
 45                   research_part / research_part_diff /
 46                   research_module_graph / research_source_drift /
 47                   research_impact / research_residency /
 48                   research_history / research_recent,
 49                   Conduit.bind_inactive
 50VERIFY: rides the owner's 3.14t harness; asserts are the contract.
 51"""
 52import importlib
 53
 54import melder as md
 55
 56
 57FRAME = "well-world"
 58MODULE_V1 = "well_pricing_v1"
 59MODULE_V2 = "well_pricing_v2"
 60
 61# TWO GENERATED MODULE WORLDS, so the comparison below has two genuinely
 62# different recorded texts to read. Two classes typed into THIS file would
 63# share one module and one snapshot - both sides of every diff would be
 64# the same bytes, and the comparison law would demonstrate nothing.
 65# Generated source is also the RELIABLE lane here: synthetic module
 66# sources are ALWAYS harvested, while user module text rides the opt-in
 67# retention lane.
 68SOURCE_V1 = '''"""Recorded pricing, first version."""
 69
 70
 71class Pricing:
 72    def __init__(self) -> None:
 73        self.rate = 10
 74
 75
 76def quote(units: int) -> int:
 77    return 10 * units
 78'''
 79
 80SOURCE_V2 = '''"""Recorded pricing, second version."""
 81
 82
 83class Pricing:
 84    def __init__(self) -> None:
 85        self.rate = 25
 86        self.surcharge = 5
 87
 88
 89def quote(units: int) -> int:
 90    return (25 * units) + 5
 91'''
 92
 93
 94def _show(label: str, value: object) -> None:
 95    """Print a read's shape without pretending to know its schema."""
 96    if isinstance(value, dict):
 97        print("  %-22s -> dict, keys: %s" % (label, sorted(value)[:5]))
 98    elif isinstance(value, list):
 99        print("  %-22s -> list, %d row(s)" % (label, len(value)))
100    else:
101        print("  %-22s -> %s" % (label, type(value).__name__))
102
103
104def main() -> None:
105    crystallizer = md.Crystallizer()
106    crystallizer.activate(
107        md.CrystallizerConfigurationBuilder().with_defaults().activate(),
108    )
109    research = md.MutationResearch()
110    configuration = research.create_configuration()
111    configuration.with_defaults().activate()
112    research.activate(configuration)
113
114    spellbook_configuration = (
115        md.SpellbookConfiguration(FRAME).with_defaults().finalize()
116    )
117    book = md.Spellbook(aetheric_frame=FRAME,
118                        configuration=spellbook_configuration)
119    book.configure_aether_frame(
120        system_state="dynamic",
121        disposal=None,
122        disposal_method_names=None,
123        rift_enabled=True,
124        ai_native=True,
125    )
126    # AN EMPTY FRAME IS A REAL FRAME. `configure_aether_frame` declares the
127    # frame's LAW; `conjure` REALIZES it by giving it a root conduit, and
128    # that realization is what publishes it to the Nexus. Publication is
129    # gated on `rift_enabled` ALONE - the spell loop it runs iterates
130    # whatever the book holds, including nothing. So the frame below is
131    # conjured EMPTY and is immediately linkable; spells are cargo, not a
132    # precondition, and everything bound after this arrives incrementally.
133    conduit = book.conjure(name="well-root")
134
135    nexus = md.Nexus()
136    system_configuration = nexus.create_configuration()
137    system_configuration.with_rift_creation_enabled(True)
138    system_configuration.with_allowed_target_frame_names([FRAME])
139    nexus.activate(system_configuration)
140    rift_configuration = nexus.create_rift_configuration()
141    rift_configuration.with_space_type("codegen")
142    rift = nexus.create_rift(configuration=rift_configuration,
143                             rift_name="weller")
144    rift.mark_active()
145    rift.create_frame_link(FRAME)
146    commands = rift.space.command_system
147
148    # Now the room can write both module worlds.
149    for module_name_to_make, source in ((MODULE_V1, SOURCE_V1),
150                                        (MODULE_V2, SOURCE_V2)):
151        commands.validate_codegen(source, frame_name=FRAME)
152        kept = commands.materialize_codegen(
153            source, module_name=module_name_to_make, frame_name=FRAME,
154        )
155        assert kept["materialized"] is True, kept
156    first = importlib.import_module(MODULE_V1)
157    second = importlib.import_module(MODULE_V2)
158
159    # TWO VERSIONS ON ONE LINEAGE, FROM TWO MODULE WORLDS - which is what
160    # gives the comparison below two genuinely different sides. The first
161    # bind lands in the already-conjured frame and publishes incrementally;
162    # the second rides its spell_index as a VERSION rather than a second
163    # visible spell, which is why two classes named `Pricing` never collide.
164    v1 = book.bind(spell=first.Pricing, existence="unique",
165                   permissions="create", binding_name="well-pricing")
166    v2 = conduit.bind_inactive(
167        spell=second.Pricing,
168        spell_index=conduit.get_spell_by_id(v1).spell_index,
169        existence="unique", permissions="create",
170    )
171    module_name = MODULE_V1
172    print("two versions recorded:", v1[:12], "and", v2[:12])
173    print("v1 module:", MODULE_V1, "| v2 module:", MODULE_V2)
174
175    print()
176    print("THE FOUR GRAINS, widest to narrowest:")
177    try:
178        _show("source(world)", commands.research_source(v1))
179        _show("source(one module)",
180              commands.research_source(v1, module_name=module_name))
181        _show("module(dossier)", commands.research_module(v1, module_name))
182        _show("parts(inventory)", commands.research_parts(v1))
183        _show("part(one lookup)",
184              commands.research_part(v1, "Pricing", kind="class"))
185    except RuntimeError as custody:
186        print("  custody read REFUSED:", str(custody)[:100])
187        print("  LOUD is correct: a silent empty read would be")
188        print("  indistinguishable from `this world has no code`")
189        return
190
191    # PARTS ARE TOP-LEVEL ONLY, and a miss is a VALUE, not an exception.
192    # `__init__` is a method, so it is invisible to this grain - and the
193    # read says so honestly rather than raising.
194    absent = commands.research_part(v1, "NoSuchPartAnywhere")
195    method = commands.research_part(v1, "__init__", kind="function")
196    assert absent["found"] is False
197    assert method["found"] is False, "parts are TOP-LEVEL; __init__ is not"
198    _show("part(absent)", absent)
199    _show("part(a method)", method)
200    print("  absence is a real result when you are exploring a world you")
201    print("  do not know - so a miss returns `found: False`, never raises")
202
203    print()
204    print("THE COMPARISON LAW - recorded material only, never the disk:")
205    function_diff = commands.research_part_diff(v1, v2, "quote",
206                                                kind="function")
207    class_diff = commands.research_part_diff(v1, v2, "Pricing", kind="class")
208    _show("part_diff(function)", function_diff)
209    _show("part_diff(class)", class_diff)
210    print("  both sides came from DIFFERENT module worlds, so this is a")
211    print("  real comparison. Two versions typed into one file would share")
212    print("  one snapshot and diff identical bytes - the law would hold and")
213    print("  demonstrate nothing")
214
215    print()
216    print("THE JOINS - a radius is only useful if you know WHO it hits:")
217    _show("module_graph", commands.research_module_graph(v1))
218    _show("impact(by spell)", commands.research_impact(spell_id=v1))
219    _show("impact(by module)",
220          commands.research_impact(module_name=module_name))
221
222    print()
223    print("THE RECORD READS - where a version lives, and what happened:")
224    _show("residency(v1)", commands.research_residency(v1))
225    _show("history(v1)", commands.research_history(v1))
226    _show("recent(limit=5)", commands.research_recent(limit=5))
227    print("  residency answers WHERE a version lives; history answers WHAT")
228    print("  HAPPENED to it; recent is the cold-landing read for an agent")
229    print("  that just arrived with no id to start from")
230
231    print()
232    _show("source_drift()", commands.research_source_drift())
233    print("  recorded-vs-disk for every sealed module, with a radius for")
234    print("  each one that moved - how a restore ANNOUNCES divergence")
235    print("  before it builds anything")
236
237    print()
238    print("four grains: world, module, inventory, part")
239    print("comparison drinks the RECORD, never the disk - two versions only")
240    print("exist at the same time in one place, and that place is the record")
241
242
243if __name__ == "__main__":
244    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 expert examples ยท Level guide

API contracts๏ƒ