# The codegen loop end to end **🔵 Expert · Lesson 12** THE CODEGEN LOOP, ACTUALLY RUN. Expert 07 introduced the room and named its three verbs. This drives them: an agent asks permission, writes code into a named world, and reads back what it did. THE LOOP validate_codegen(code, frame_name=...) may I? execute_codegen(code, frame_name=...) do it materialize_codegen(...) make it durable A REFUSAL IS A RETURN VALUE, NOT AN EXCEPTION. `validate_codegen` hands back a `CodegenValidationResult`. Rejected code does not raise - and on the execute path a rejected validation returns a VALIDATION-FAILED `CodegenExecutionResult` without ever reaching compile or exec. An agent reads a verdict; it does not catch one. (Exceptions are reserved for malformed REQUESTS: empty code or an empty frame name raise `ValueError`. Bad input raises, bad code reports.) VALIDATE IS A SEPARATE VERB, AND THAT IS THE WHOLE DESIGN. An agent can ask "would this be permitted" WITHOUT running anything. And `execute_codegen` VALIDATES FIRST anyway - the invariant is "validation runs before execution on the execute path", so the separate verb buys you the answer early, never a way around it. Every other safe-execution story collapses those into one call and forces you to attempt the thing to learn whether you were allowed to attempt it - which means the only way to discover a boundary is to cross it. WHAT `validate` ACTUALLY ANSWERS Not "is this good code". It answers "does this code stay inside the posture this room was granted", against seven strategies: ast_structure, import_policy, builtin_policy, attribute_access, name_resolution, reflection_policy, recursive_control. Read `recursive_control` twice. It exists because generated code that generates code is how a bounded system stops being bounded. Someone thought about an agent escaping its sandbox by writing a smaller one inside it. THE ROOM REMEMBERS THE SOURCE, AND IT PUSHES Memory is not a describe-door you pull from the command system - it is `room.memory_system`, a `RiftMemorySystem`, and you SUBSCRIBE: room.memory_system.register_memory_callback(fn) -> subscription id `memory_enabled` is literally "does anyone have a callback registered", so emission costs nothing until something is listening. A paraphrase would be useless afterwards anyway - the question you ask later is always "what EXACTLY did it write". AND THE RECORD IS ONE PER SUCCESSFUL TOP-LEVEL CALL. Two details make that precise, and both matter: - SUCCESSFUL. A refused command does not emit. Memory is a log of what happened, not of what was attempted. - TOP-LEVEL. The command system keeps a nested call-depth counter precisely to SUPPRESS duplicate emission when one public command calls another internally. You get the call you made, not the call tree underneath it. frame_name IS REQUIRED ON BOTH. Generated code has to land in a named world and there is no sane default for that - a codegen call that guessed its target frame would be the worst possible bug. ## Before you run Use the [Expert guide](../../expert/index.md) 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 ```bash python UX_and_AIX_experiences/04_expert/12_the_codegen_loop_end_to_end.py ``` ```powershell py -3.14t UX_and_AIX_experiences/04_expert/12_the_codegen_loop_end_to_end.py ``` {download}`Download this collection <../../downloads/expert-examples.zip>` · [Source on GitHub](https://github.com/Synaptic724/melder/blob/0895ce795453cd87e35d70665957b4e89aad3317/UX_and_AIX_experiences/04_expert/12_the_codegen_loop_end_to_end.py) ## Public surface CodegenCommandSystem.validate_codegen / execute_codegen / research_preview, the CodegenValidationResult / CodegenExecutionResult verdicts, and RiftSpace.memory_system callbacks. materialize_codegen is DESCRIBED here, not called - the third rung is a separate decision and expert 26 takes it; running is not keeping. ## Code ```{literalinclude} ../../downloads/04_expert/12_the_codegen_loop_end_to_end.py :language: python :linenos: ``` ## 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](index.md) · [Level guide](../../expert/index.md) ## Related guides - [Build a working system from generated code](../../expert/codegen.md) ## API contracts - [RiftSpace](../../reference/api/inspection/riftspace.md)