Recursive Code World Models
- Recursive Code World Models (RCWMs) are systems using executable code to model, predict, simulate, or transform structured world states recursively.
- RCWMs encompass methodologies like recursively executable transition programs, LLM-generated code world models, and recursive language-model calls, essential for applications in AI planning and simulation of complex environments (e.g., AI code generation).
- Key aspects of RCWMs involve executable transition programs, recursive computation for long-horizon control, and code generation with iterative model-evolution.
Recursive Code World Models (RCWMs) are systems in which executable code represents, predicts, simulates, or transforms structured world states, while recursive computation enables repeated state updates, hierarchical decomposition, self-correction, program revision, or nested model invocation. The term encompasses several related but non-equivalent paradigms: recursively executable transition programs, LLM-generated code world models, recursive language-model calls, recursive agent harnesses, code-grounded simulators, and memory systems that evolve through verified feedback. No single paper in the surveyed literature defines RCWM as a unified formal architecture. Rather, RCWM is an Editor's term for the intersection of executable world modeling, code generation, recursive computation, and closed-loop planning.
1. Conceptual foundations and scope
A conventional world model approximates how a system evolves under actions. In a deterministic setting, this can be expressed as
where is the current state and is an action. An RCWM extends this arrangement by representing , parts of , or the process of constructing as executable or recursively invoked code. The model may therefore be used to generate hypothetical trajectories, decompose a world into subproblems, revise its own transition mechanisms, or maintain state outside the neural context.
Several distinctions are necessary.
Recursive execution repeatedly applies an existing transition program, as in
Recursive model generation produces a new world model from a previous model and feedback, for example through a generate–verify–repair loop. Recursive reasoning invokes a model or agent on isolated subtasks and combines the returned results. Iterative refinement repeatedly maps a candidate solution to an improved candidate, without necessarily invoking recursive code or simulating an external environment.
These mechanisms should not be conflated. GIF-MCTS constructs executable Code World Models through iterative tree-structured program synthesis, but it does not implement strict recursive world-model composition (Dainese et al., 2024). See-Saw alternates updates to main code and dependencies, but its experiments do not establish execution-based world simulation (Vsevolodovna, 2024). R-Qwen recursively refines serialized candidate solutions, but does not model action-conditioned environment transitions (Manzari et al., 2 Aug 2026). Recursive LLMs externalize long prompts and recursively query submodels, but primarily operate over textual inputs (Zhang et al., 31 Dec 2025).
A strong RCWM interpretation requires at least some of the following:
- an explicit or inspectable state representation;
- action-conditioned transitions;
- executable code or a constrained program representation;
- repeated or nested simulation;
- environment, verifier, or execution feedback;
- persistence across time;
- mechanisms for branching, correction, or model revision.
The strongest systems additionally require typed state and action interfaces, transactional execution, uncertainty management, provenance, sandboxing, and validation of generated code.
2. Executable transition programs and code-grounded dynamics
The most direct RCWM formulation represents dynamics as executable source code over structured state. VisualPatchWorld (VPW) uses an object-centric scene graph and induces a transition program
where is executable code and 0 contains fitted numerical parameters (Bai et al., 28 Jul 2026). The model is recursively rolled out over action sequences and embedded in model-predictive control.
VPW separates qualitative dynamics from quantitative parameter fitting. A hypothesis 1 determines the qualitative form of the transition—such as joint-space dynamics, grip-gated contact, or quasi-static pushing—while 2 specifies numerical quantities. Short active probes select a qualitative form, after which multistep rollout loss fits its parameters. This separation addresses a central failure mode of code-based modeling: a program can fit local transitions while encoding the wrong mechanism.
The induced transition program is used in a closed loop:
3
VPW evaluates induced-only, hybrid, and simulator-scored planning. In hybrid planning, candidate action sequences are first scored by the induced code model, after which the top 30% are re-scored in MuJoCo. Under oracle state, VPW’s induced planning mean is 69.0%, compared with 45.5% for POMDP-Coder; the strongest gains occur on Reacher, Cube, and PushT (Bai et al., 28 Jul 2026). On PushT, induced-only success is 22%, while oracle hybrid scoring reaches 96%, illustrating the sensitivity of planning to contact-model errors.
A separate line of work constructs verified symbolic CWMs. World-Time Compute with Verified Code World Models defines a world as
4
where 5 is a JSON-serializable state space, 6 is a discrete action set, 7 is a deterministic transition program, 8 and 9 define objectives, and 0 specifies invariants (Schwoebel et al., 7 Jul 2026). Illegal actions are represented as no-ops, and random seeds may be threaded through the state to preserve replayability.
Its construction pipeline is:
1
The verified programs achieved 24/24 exact 20-step rollouts across the principal Orchard, Sprint, and Triage worlds, whereas a per-step LLM predictor achieved 0/24 exact rollouts. On ten-times out-of-distribution probes, verified symbolic code achieved 100% exactness, while the reported MLP and nearest-neighbor systems collapsed to 0% at that scale. These results support executable symbolic transitions as a mechanism for avoiding per-step prediction error accumulation, although they do not establish semantic equivalence to an intended specification.
The same work distinguishes code synthesis from code induction. When rules are explicitly supplied, synthesized code can recover exact behavior. When only transition examples are supplied, code induction achieves 0.43 in-distribution and 0.43 ten-times OOD probe accuracy, compared with 0.40 and 0.00 for an MLP and 0.17 and 0.00 for 1-NN. The difference indicates that executable representation alone does not solve identifiability: passive trajectories may fail to expose the relevant interaction rule.
3. Recursive computation, decomposition, and long-horizon control
Recursive LLMs provide a general mechanism for moving large inputs outside the model context and recursively processing selected subproblems (Zhang et al., 31 Dec 2025). A root model operates over an external Python environment containing the full prompt, persistent variables, and an llm_query function. It can slice, filter, parse, batch, and recursively submit snippets to submodels.
The external environment separates bounded neural context from persistent program state:
2
3
4
This architecture is directly relevant to RCWM because a controller can use the same arrangement to inspect world state, decompose a program or trajectory, invoke local world-model predictions, preserve intermediate hypotheses, and aggregate results. RLM experiments report substantial gains on dense aggregation tasks, including GPT-5 OOLONG-Pairs performance increasing from 0.04 for the base model to 58.00 for RLM.
The formal work on Recursive Models for Long-Horizon Reasoning distinguishes a single growing context from a recursive context stack (Yang et al., 2 Mar 2026). A recursive model maintains suspended parent contexts and an active child context. A generated call creates an isolated subtask; a generated return pops the child and passes a result to the parent. The paper’s theoretical model separates local space—the largest active context—from global space—the total size of active and suspended contexts.
Its main computational claim is that unbounded recursive depth can provide exponential-time computational power with locally bounded contexts:
5
Constant-depth recursion does not obtain the same separation and remains within a space-like regime. These results concern idealized bounded-precision Transformer constructions and do not establish that a finite trained model will discover useful decompositions. For RCWMs, the practical implication is that recursive execution can separate active reasoning context from total computation, but it does not remove simulator cost, branch explosion, or state-consistency requirements.
Recursive Agent Harnesses extend model recursion by making a full tool-using agent harness the recursive unit (Lumer et al., 11 Jun 2026). A child harness receives a task, workspace, filesystem and shell tools, context management, and potentially the ability to spawn further children. Large workloads are distributed through executable Python scripts using asyncio.gather; smaller workloads can use structured Task calls.
RAH improves a GPT-5 coding-agent baseline from 71.75% to 81.36% on Oolong-Synthetic, while Claude Sonnet 4.5 reaches 89.77% with the same design. The architecture is relevant to RCWM as an execution substrate: code controls decomposition, concurrency, workspace allocation, tool invocation, and aggregation. It does not itself provide a physical or computational world simulator. Its filesystem is an external memory and communication interface, not a learned causal state.
For an RCWM, recursive decomposition must be combined with explicit state semantics. Independent child simulations require snapshots, scoped permissions, transaction-local state, or copy-on-write branches. A child result should include its assumed parent-state version and a typed state delta. The parent should commit the delta only if its preconditions remain valid.
4. Code generation, repair, and recursive model evolution
GIF-MCTS constructs deterministic Code World Models as executable Python Environment classes with __init__, set_state, and step methods (Dainese et al., 2024). The generated program approximates an environment’s transition, reward, and termination mechanisms. Candidate programs are tested against offline transitions, and the resulting accuracy determines their value in a Monte Carlo Tree Search.
The three principal synthesis actions are:
- Generate: extend a partial program;
- Improve: revise a complete but incorrect program using counterexample feedback;
- Fix: repair syntactic or runtime failures.
Candidates are represented as a state prefix plus a rollout completion. This allows MCTS to search over code blocks rather than individual tokens. The candidate value is the fraction of tested transitions for which predicted next state, reward, and termination flag match the reference.
On the Code World Models Benchmark, GIF-MCTS with Llama 3 achieves discrete transition accuracy of 6 and normalized return of 7, compared with 8 and 9 for WorldCoder. With GPT-4 Turbo, it achieves 0 discrete accuracy and 1 normalized return, compared with 2 and 3. The CWM itself is extremely fast once synthesized: reported step times are 0.00005 seconds for CartPole-v1, 0.0001 seconds for HalfCheetah-v4, and 0.0001 seconds for Humanoid-v4, although construction requires substantial LLM computation.
GIF-MCTS is iterative and tree-structured, but it is not strict recursive world-model generation. The search tree contains alternative candidate programs; it does not recursively invoke a world-model generator inside a generated world, nor does it compose nested executable world models.
See-Saw provides a second model-evolution pattern. A project is represented as a main component 4 and dependencies 5:
6
7
An alignment validator 8 determines whether the resulting project is coherent (Vsevolodovna, 2024). This is a recursive consistency-maintenance process over code snapshots, but the paper does not provide a formal dependency graph, execution-grounded validator, convergence guarantee, rollback system, or demonstrated scaling beyond approximately 30 files. See-Saw therefore supplies an orchestration pattern rather than a complete behavioral code world model.
The Code World Model released as a 32-billion-parameter open-weights LLM provides another important foundation (team et al., 30 Sep 2025). It is trained on Python interpreter traces, Dockerized repository trajectories, compiler and kernel data, Lean state/action/result triples, and multi-turn agentic interactions. Python trace prediction represents local variables, stack-frame metadata, source lines, calls, returns, and exceptions. The model achieves 96.9% state exact match and 96.5% action exact match on CruxEval trace analysis, with 88.0% output pass@1 in the reported full-trace setting.
CWM supports a learned transition predictor over serialized execution traces, but it does not implement a dedicated recursive planner over its own predictions. The paper identifies planning with code world models and formal integration of trace prediction into reasoning as future work. In RCWM terms, CWM supplies a learned execution-dynamics layer; recursive branching, verification, model-predictive control, and error correction remain additional mechanisms.
5. Structured environments, memory, and state consistency
Web World Models separate a deterministic Physics Layer from an LLM-generated Imagination Layer (Feng et al., 29 Dec 2025):
9
0
1
Typed interfaces, JSON schemas, deterministic procedural generation, caching, and fallback templates maintain structural consistency while LLMs generate descriptions, dialogue, missions, narratives, or content proposals. AI Alchemy is the clearest self-extending example: when an unseen material pair collides, the LLM proposes a reaction, which is validated, cached, and integrated into the simulator. AI Spire similarly generates cards and relics, but their effects are constrained to a pre-existing symbolic effect vocabulary.
These systems are compositional and sometimes self-extending, but they do not generally generate new executable world-model code. They demonstrate a useful RCWM design principle: generated semantics should be mediated by typed contracts and interpreted by a trusted execution layer.
The later Code World Model framework, Code World Model: Coding Agent as World Brain, more directly treats code as a persistent causal substrate (Chen et al., 26 Aug 2026). It separates executable world state from visual realization:
2
The executable component contains programs, entities, relations, rules, event history, memory, and off-screen state. The system converts selected executable state into a proxy representation, then conditions a video model on the proxy and structured text. The coding agent performs high-level reasoning and code modification; executable code performs frequent low-level updates such as collisions, movement, cooldowns, schedules, and event propagation.
This architecture supports a hierarchy of
3
Its demonstrated visual results concern proxy-to-video control rather than long-horizon recursive self-modification. The main unresolved problem is semantic-rendering consistency: a generated video may visually violate the executable state, and the reported system does not provide a verifier that compares generated frames against symbolic consequences.
Recuris addresses a complementary problem: memory control for long-horizon agents (Yu et al., 25 Aug 2026). It maintains Experiential Memory, Working Memory, an invocation policy, and checkers. Working Memory records pending, completed, or blocked goals together with evidence. Structured traces record states, retrieved skills, actions, observations, proposed updates, checker decisions, and committed updates.
The memory evolution loop is:
4
where 5 is the memory package, 6 is an execution trace, 7 is a failure attribution, and 8 is a localized patch. On four long-horizon benchmarks, Recuris improves 35 of 37 completed model–benchmark pairs. Its fault-localization accuracy rises from 13.0% using outcome alone to 64.8% using structured traces.
Recuris is not an executable world model. Its memory state is a compact task representation rather than a complete environmental state, and its skills are not generally transition programs. Its contribution to RCWM is methodological: state-grounded retrieval, evidence-gated transitions, structured traces, localized repair, validation, and rollback-like admission control.
A formal theory of world-model channels further distinguishes environment, agent, and joint-process models (Baltieri et al., 23 Jul 2026). The environment channel predicts observations conditioned on actions:
9
The agent channel predicts actions conditioned on observations:
0
The joint model predicts the realized action–observation process:
1
This distinction is critical for RCWMs. A model that predicts the current agent’s on-policy behavior may not support counterfactual action queries. A joint model may be compact because the policy suppresses many theoretically possible continuations. The paper’s support-restricted factorization theorem shows that the on-coupling environment model can be a quotient of the joint causal state, but this compactness does not guarantee counterfactual validity for actions outside the realized support.
6. Architectures, evaluation, and unresolved problems
Recursive candidate refinement provides a third relevant mechanism. R-Qwen repeatedly maps an initial structured problem and current candidate solution to a new candidate:
2
Immutable positions are clamped and malformed values are replaced by previous values (Manzari et al., 2 Aug 2026). Deep supervision applies losses at multiple recursion steps, while Hierarchical Supervision Weighting assigns larger weights to early refinement steps:
3
R-Qwen achieves 88.5% on Sudoku, 93.1% on Maze, 82.1% on ARC-AGI-1, and 33.5% on ARC-AGI-2 in the reported comparison. Its recursion is a single candidate trajectory without branching, rollback, environment interaction, or executable code. It is therefore a recursive solver rather than a full RCWM, but its explicit state serialization, invariant projection, curriculum over recursion depth, and deep supervision are applicable to RCWM controllers.
The study of Tiny Recursive Models for code generation emphasizes that recursive architectures must be evaluated both under teacher forcing and free-running generation (Sirivella et al., 29 Aug 2026). TRM-AR uses a shared two-layer block repeatedly, attaining effective depth 20 with approximately 27.8 million parameters. It outperforms a parameter-matched two-layer transformer but underperforms a depth-matched 20-layer transformer at its validation optimum. TRM-AR recovers approximately 45% of the validation-loss gap and 57% of the generation-quality gap between the two baselines, at approximately 175 times the per-step cost of the parameter-matched model.
The result suggests that recursive weight sharing can improve parameter efficiency and resistance to overfitting without providing greater raw capability than a fully parameterized deep transformer. For RCWMs, evaluation should therefore separate parameter count, effective depth, free-running code quality, execution success, latency, memory, and training cost.
Cortex addresses the systems problem of compiling recursive neural computation (Fegade et al., 2020). It separates recursive control flow from tensor computation, inspects a runtime structure, linearizes it into arrays and batches, and executes compiled irregular loops. Its Recursive API lowers to Irregular Loop IR, which supports indirect indexing, variable bounds, conditional operators, named dimensions, and layout transformations.
Cortex supports sequences, trees, DAGs, and loop-carried computations under structural restrictions. Its optimizations include dynamic batching, conditional specialization, computation hoisting, loop transformations, recursive unrolling, recursive refactoring, kernel fusion, model persistence, dense scratchpad layouts, barrier placement, loop peeling, and backend-specific transformations. The abstract reports up to 14-times lower inference latency than prior work across GPU, Intel CPU, and ARM CPU backends. Its TreeLSTM profiling example uses one kernel call compared with 389 for DyNet and 122 for Cavs.
Cortex is not a compiler for arbitrary code-generating world models. It is most applicable to RCWMs whose execution can be represented as repeated tensor updates over inspectable trees, sequences, or DAGs. It is less applicable to arbitrary side effects, value-dependent control flow, dynamic allocation, unbounded loops, aliasing, or opaque code semantics. Nevertheless, its central principle—inspect structure, regularize control flow, and compile the neural semantics—is directly relevant to low-latency RCWM execution.
A complete RCWM would need to combine these strands while addressing unresolved technical problems:
- State representation: use typed, versioned, provenance-aware state rather than unrestricted text or incomplete summaries.
- Executable semantics: distinguish candidate code, compiled code, simulator state, observations, and verified effects.
- Recursive control: support call/return composition, task decomposition, branching, bounded recursion, and termination.
- Transactional simulation: execute speculative branches over snapshots or copy-on-write states and commit only validated deltas.
- Counterfactual validity: distinguish on-policy joint modeling from environment modeling under arbitrary interventions.
- Verification: combine parsing, type checking, sandbox execution, invariants, branch-covering probes, held-out tests, and differential comparison with an authoritative simulator.
- Uncertainty: represent uncertainty over code, state, transitions, and observations; token likelihood alone is insufficient for state-level confidence.
- Model revision: use structured traces to localize failures to code, state representation, retrieval, transition logic, or verification.
- Training objectives: combine code likelihood, transition prediction, execution correctness, multistep stability, planning value, and recursive-control losses.
- Evaluation: report teacher-forced fit, free-running generation, executable correctness, multistep rollout accuracy, OOD probes, planning success, latency, memory, and total computation.
- Safety: enforce sandboxing, capability restrictions, resource quotas, network controls, rollback, versioning, and provenance for generated code.
- Perception and grounding: connect visual or textual observations to symbolic state without assuming that semantic recognition alone provides metrically accurate dynamics.
The most defensible definition of an RCWM is therefore a code-mediated, recursively executable system in which structured state, actions, transition programs, execution feedback, and model revision form a closed computational loop. Existing work establishes important components but not a single complete solution. Executable simulators demonstrate inspectable and verifiable dynamics; recursive LLMs demonstrate externalized computation and nested calls; agent harnesses demonstrate recursive tool-using orchestration; code-generation systems demonstrate synthesis and repair; typed web worlds demonstrate persistent rule-governed environments; memory-evolution systems demonstrate localized adaptation; and recursive neural models demonstrate parameter sharing and iterative correction.
A full RCWM would integrate these capabilities into a system that can generate or modify executable world components, simulate their consequences recursively, verify them against observations and invariants, maintain persistent state, and improve its own models without sacrificing causal consistency or safety.