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: RUN GREEN 2026-10-25 on 3.14t, GIL off and on; asserts are the
 51        contract.
 52"""
 53import importlib
 54
 55import melder as md
 56
 57
 58FRAME = "well-world"
 59MODULE_V1 = "well_pricing_v1"
 60MODULE_V2 = "well_pricing_v2"
 61
 62# TWO GENERATED MODULE WORLDS, so the comparison below has two genuinely
 63# different recorded texts to read. Two classes typed into THIS file would
 64# share one module and one snapshot - both sides of every diff would be
 65# the same bytes, and the comparison law would demonstrate nothing.
 66# Generated source is also the RELIABLE lane here: synthetic module
 67# sources are ALWAYS harvested, while user module text rides the opt-in
 68# retention lane.
 69SOURCE_V1 = '''"""Recorded pricing, first version."""
 70
 71
 72class Pricing:
 73    def __init__(self) -> None:
 74        self.rate = 10
 75
 76
 77def quote(units: int) -> int:
 78    return 10 * units
 79'''
 80
 81SOURCE_V2 = '''"""Recorded pricing, second version."""
 82
 83
 84class Pricing:
 85    def __init__(self) -> None:
 86        self.rate = 25
 87        self.surcharge = 5
 88
 89
 90def quote(units: int) -> int:
 91    return (25 * units) + 5
 92'''
 93
 94
 95def _show(label: str, value: object) -> None:
 96    """Print a read's shape without pretending to know its schema."""
 97    if isinstance(value, dict):
 98        print("  %-22s -> dict, keys: %s" % (label, sorted(value)[:5]))
 99    elif isinstance(value, list):
100        print("  %-22s -> list, %d row(s)" % (label, len(value)))
101    else:
102        print("  %-22s -> %s" % (label, type(value).__name__))
103
104
105def main() -> None:
106    crystallizer = md.Crystallizer()
107    crystallizer.activate(
108        md.CrystallizerConfigurationBuilder().with_defaults().activate(),
109    )
110    research = md.MutationResearch()
111    configuration = research.create_configuration()
112    configuration.with_defaults().activate()
113    research.activate(configuration)
114
115    spellbook_configuration = (
116        md.SpellbookConfiguration(FRAME).with_defaults().finalize()
117    )
118    book = md.Spellbook(aetheric_frame=FRAME,
119                        configuration=spellbook_configuration)
120    book.configure_aether_frame(
121        system_state="dynamic",
122        disposal=None,
123        disposal_method_names=None,
124        rift_enabled=True,
125        ai_native=True,
126    )
127    # AN EMPTY FRAME IS A REAL FRAME. `configure_aether_frame` declares the
128    # frame's LAW; `conjure` REALIZES it by giving it a root conduit, and
129    # that realization is what publishes it to the Nexus. Publication is
130    # gated on `rift_enabled` ALONE - the spell loop it runs iterates
131    # whatever the book holds, including nothing. So the frame below is
132    # conjured EMPTY and is immediately linkable; spells are cargo, not a
133    # precondition, and everything bound after this arrives incrementally.
134    conduit = book.conjure(name="well-root")
135
136    nexus = md.Nexus()
137    system_configuration = nexus.create_configuration()
138    system_configuration.with_rift_creation_enabled(True)
139    system_configuration.with_allowed_target_frame_names([FRAME])
140    nexus.activate(system_configuration)
141    rift_configuration = nexus.create_rift_configuration()
142    rift_configuration.with_space_type("codegen")
143    rift = nexus.create_rift(configuration=rift_configuration,
144                             rift_name="weller")
145    rift.mark_active()
146    rift.create_frame_link(FRAME)
147    commands = rift.space.command_system
148
149    # Now the room can write both module worlds.
150    for module_name_to_make, source in ((MODULE_V1, SOURCE_V1),
151                                        (MODULE_V2, SOURCE_V2)):
152        commands.validate_codegen(source, frame_name=FRAME)
153        kept = commands.materialize_codegen(
154            source, module_name=module_name_to_make, frame_name=FRAME,
155        )
156        assert kept["materialized"] is True, kept
157    first = importlib.import_module(MODULE_V1)
158    second = importlib.import_module(MODULE_V2)
159
160    # TWO VERSIONS ON ONE LINEAGE, FROM TWO MODULE WORLDS - which is what
161    # gives the comparison below two genuinely different sides. The first
162    # bind lands in the already-conjured frame and publishes incrementally;
163    # the second rides its spell_index as a VERSION rather than a second
164    # visible spell, which is why two classes named `Pricing` never collide.
165    # It is staged under the same binding name: every version of an index
166    # is bound the way its first one was, so both share one address
167    # (2026-10-25).
168    v1 = book.bind(spell=first.Pricing, existence="unique",
169                   permissions="create", binding_name="well-pricing")
170    v2 = conduit.bind_inactive(
171        spell=second.Pricing,
172        spell_index=conduit.get_spell_by_spell_id(v1).spell_index,
173        binding_name="well-pricing",
174        existence="unique", permissions="create",
175    )
176    module_name = MODULE_V1
177    print("two versions recorded:", v1[:12], "and", v2[:12])
178    print("v1 module:", MODULE_V1, "| v2 module:", MODULE_V2)
179
180    print()
181    print("THE FOUR GRAINS, widest to narrowest:")
182    try:
183        _show("source(world)", commands.research_source(v1))
184        _show("source(one module)",
185              commands.research_source(v1, module_name=module_name))
186        _show("module(dossier)", commands.research_module(v1, module_name))
187        _show("parts(inventory)", commands.research_parts(v1))
188        _show("part(one lookup)",
189              commands.research_part(v1, "Pricing", kind="class"))
190    except RuntimeError as custody:
191        print("  custody read REFUSED:", str(custody)[:100])
192        print("  LOUD is correct: a silent empty read would be")
193        print("  indistinguishable from `this world has no code`")
194        return
195
196    # PARTS ARE TOP-LEVEL ONLY, and a miss is a VALUE, not an exception.
197    # `__init__` is a method, so it is invisible to this grain - and the
198    # read says so honestly rather than raising.
199    absent = commands.research_part(v1, "NoSuchPartAnywhere")
200    method = commands.research_part(v1, "__init__", kind="function")
201    assert absent["found"] is False
202    assert method["found"] is False, "parts are TOP-LEVEL; __init__ is not"
203    _show("part(absent)", absent)
204    _show("part(a method)", method)
205    print("  absence is a real result when you are exploring a world you")
206    print("  do not know - so a miss returns `found: False`, never raises")
207
208    print()
209    print("THE COMPARISON LAW - recorded material only, never the disk:")
210    function_diff = commands.research_part_diff(v1, v2, "quote",
211                                                kind="function")
212    class_diff = commands.research_part_diff(v1, v2, "Pricing", kind="class")
213    _show("part_diff(function)", function_diff)
214    _show("part_diff(class)", class_diff)
215    print("  both sides came from DIFFERENT module worlds, so this is a")
216    print("  real comparison. Two versions typed into one file would share")
217    print("  one snapshot and diff identical bytes - the law would hold and")
218    print("  demonstrate nothing")
219
220    print()
221    print("THE JOINS - a radius is only useful if you know WHO it hits:")
222    _show("module_graph", commands.research_module_graph(v1))
223    _show("impact(by spell)", commands.research_impact(spell_id=v1))
224    _show("impact(by module)",
225          commands.research_impact(module_name=module_name))
226
227    print()
228    print("THE RECORD READS - where a version lives, and what happened:")
229    _show("residency(v1)", commands.research_residency(v1))
230    _show("history(v1)", commands.research_history(v1))
231    _show("recent(limit=5)", commands.research_recent(limit=5))
232    print("  residency answers WHERE a version lives; history answers WHAT")
233    print("  HAPPENED to it; recent is the cold-landing read for an agent")
234    print("  that just arrived with no id to start from")
235
236    print()
237    _show("source_drift()", commands.research_source_drift())
238    print("  recorded-vs-disk for every sealed module, with a radius for")
239    print("  each one that moved - how a restore ANNOUNCES divergence")
240    print("  before it builds anything")
241
242    print()
243    print("four grains: world, module, inventory, part")
244    print("comparison drinks the RECORD, never the disk - two versions only")
245    print("exist at the same time in one place, and that place is the record")
246
247
248if __name__ == "__main__":
249    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๏ƒ