Optimizing the Whole Repair Loop: Joint Prompt Search for Executor-Guided Code Refinement

When a language model writes code, runs it, reads the error or the test result, and tries again, its behavior is governed by several prompts at once. One prompt tells it how to write the first attempt. Another tells it how to read the executor's feedback. A third tells it how to patch the program, or whether to throw it away and start over. Today these prompts are written by hand, or tuned one at a time. Yet the first prompt decides which mistakes the repair prompt will ever see. We propose to treat the prompts of a multi-round refinement loop as one object and search over them jointly. The score is the only signal the loop needs: whether the final program passes its tests or produces the verified answer.

The practical stake is cost. The usual way to make a model better at fixing its own code is reinforcement learning against a program checker. This approach is often called reinforcement learning with verifiable rewards (RLVR): the model is rewarded 0 or 1 by an interpreter or unit test. It requires weight updates and a supply of problems with checkers, which often have to be synthesized. A prompt-level search touches no weights. It needs only a few dozen checked problems and pays for its signal in executor calls. If joint prompt search over the loop closes much of the gap to weight training, at a fraction of the data and compute, that changes what a practitioner should try first.

Core Claim

Our central hypothesis is that the generation prompt that is best inside a repair loop differs from the generation prompt that is best for a single attempt. We expect the loop-optimal prompt to favor code that is easy to diagnose, for example code with intermediate prints, assertions, and small functions. Its single-shot accuracy may be lower, but its final accuracy after repair is higher. If this is true, then optimizing loop components separately is not just suboptimal but systematically misdirected. A joint search that scores whole trajectories with the executor will find configurations that per-round optimization cannot reach.

A second claim follows. The decision to patch or restart can be expressed as a prompt-level rule and optimized jointly with the other prompts, instead of being learned into the weights.

This extends the joint prompt optimization literature in one specific direction. That literature has shown that prompt parts interact. SEE finds that appending separately chosen examples to an already optimized instruction hurts performance. InstructEval shows that choosing instructions and demonstrations separately reduces quality and robustness. EASE and SMILE co-select instructions and demonstrations. P3 co-optimizes system and user prompts. These papers motivate our hypothesis, but all of them work on prompts consumed in a single forward pass. The research file names "template and workflow rigidity" as an open problem: current optimizers cannot reconfigure multi-step scratchpads. In a refinement loop, the interaction between parts is not inside one context window. It runs across time, through the executor's output. That cross-round coupling is what we study.

Research Program

The object we optimize is a loop configuration with four parts. The first is a generation instruction with optional exemplars. The second is a feedback-reading instruction that turns raw tracebacks or test diffs into a diagnosis. The third is a repair instruction with exemplars of the form (buggy program, feedback, fix). The fourth is a restart rule, written in natural language, that tells the model when to discard the current program and solve again. Restarting starts a fresh attempt from the generation prompt.

The restart rule borrows its motivation from Re². That work finds that flawed early reasoning creates a "point of no return," and it trains a re-solve action with RLVR. We keep Re²'s insight but move the action from the weights into the prompt.

A configuration is scored by running the loop for at most $K$ rounds on a small problem set and measuring final verified success, minus a penalty per generated token so that longer loops do not win for free.

The search reuses existing machinery rather than inventing a new optimizer. We use an island-based evolutionary search in the style of C-Evolve, which evolves groups of prompts. A mutation edits one component, either rewriting its text or swapping its exemplars, but the configuration is always evaluated as a whole.

The one methodological addition is caching to keep costs down. When only the repair or restart parts change, we replay cached first-round programs. When the generation prompt changes, the cache is invalidated, because the population of failures the repair prompt sees has shifted. Logging which mutations invalidate the cache also gives us, for free, a direct measure of how strongly the rounds are coupled.

The analysis has three parts. First, we measure the interaction effect: the final accuracy of the jointly found configuration minus that of a configuration assembled from separately optimized parts, evaluated under the same budget. Second, we test the "write for repair" hypothesis directly. We compare the single-shot accuracy and the post-repair accuracy of the best generation prompt found by single-shot search and by loop search. The hypothesis predicts that their rankings cross. Third, we track data and compute overhead end to end. This includes the cost of creating checked problems, which makes the comparison against weight training honest. Reinforcement Learning for Reasoning in Large Language Models with One Training Example shows that RLVR can generalize from a single verified problem. That constrains our cost argument: weight training may need far less synthetic data than assumed, so we report results across training-set sizes from 1 to roughly 100 problems for both approaches.

The domain also tests a known negative result. Learning to Ask May Be Better Than Learning to Answer trains a prompter with Group Relative Policy Optimization (GRPO), a reinforcement learning method that scores each sample against the others in its group. That prompter improves retrieval workflows on HoVer and HotPotQA but degrades GSM8K, where success depends on the model's internal arithmetic. We predict this failure reverses when the math is moved out of the model and into executable programs checked by an interpreter. The prompt then governs a workflow it can control, rather than reasoning it cannot. Running GSM8K in program form is therefore a direct test of the boundary that paper found.

Baselines

The naive reference point is the loop with fixed, hand-written generic prompts for all components, run for the same $K$ rounds. Alongside it we run the same model's single attempt with no refinement. Together these show whether refinement helps at all on each benchmark.

The most important ablation is the greedy per-round optimizer, and it isolates our key ingredient. It uses the same evolutionary search and the same total executor budget. It first optimizes the generation prompt for single-shot accuracy, then freezes it and optimizes the repair and restart parts on its failures. Beating this baseline by a margin larger than the seed-to-seed variance is the test of the central claim. A second ablation removes the restart rule, which tests whether prompt-level restart contributes anything beyond repair.

The strongest existing prompt-level competitor applies existing joint instruction-and-exemplar optimization to the generation prompt alone. We use SMILE's selection for this, plus a MIPRO-style joint search, with the rest of the loop left generic. We give it the same problem set and evaluation-call budget. For reference, SMILE reports an average score of 63.0 on a Qwen3-4B backbone across five tasks, against 59.8 for the strongest joint baseline (APE with random selection). C-Evolve reports average gains of 13.85% for Qwen3-8B and 16.09% for GPT-4.1-mini over baseline prompts. Neither result is on code refinement, and we found no reported result on Lacuna for any joint prompt optimizer on executor-guided code repair.

The strongest weight-training competitor is Re² trained on the same base model. It reports raising Qwen2.5-7B-Instruct on AIME 2025 from 8.6% to 21.2%. We run our method on the same model and benchmark so the comparison is direct. We report accuracy and also total cost, measured as GPU-hours plus executor calls plus problem-creation cost. "Beating" Re² here means matching most of its gain at a fraction of its cost, not exceeding it outright.

Why not just…?

Why not just sample many independent attempts and keep one that passes? This is the most serious threat. Resampling with test-based filtering often matches repair loops at equal compute, and if it does here, the loop itself is the wrong object to optimize. We will therefore match token budgets exactly between the optimized loop and best-of-$n$ sampling. That best-of-$n$ uses its own jointly optimized generation prompt, so the comparison does not favor the loop. If best-of-$n$ wins, we will report that joint loop optimization is not worth it. If the restart-heavy configurations the search finds behave like disguised resampling, the restart-ablation will show it.

Why not just fine-tune with RLVR, as Re² does? Weight training is likely to give the highest ceiling. The question is cost and portability. Prompt configurations transfer to closed models and need no training infrastructure. Our cost-matched comparison at small training-set sizes, informed by the one-example RLVR result, decides whether the prompt route is competitive or merely cheaper and worse.

Why not just optimize each prompt separately with a good single-prompt optimizer? This is our greedy ablation. The "write for repair" ranking reversal, if it appears, explains why separate optimization should fail. If the reversal does not appear and greedy optimization matches joint search, our central claim is false, and we would report it as such.

Proposed Experiments and Decisive Checks

We run the experiments on GSM8K and AIME 2025 in program-of-thought form, where the model writes a program that computes the answer and the reward is exact match of the executed result. We add a unit-tested Python code-generation benchmark, for which we found no reported Lacuna numbers, to cover repair driven by test feedback. The models are Qwen2.5-7B-Instruct (to compare with Re²) and a Qwen3 model (to connect with the SMILE and C-Evolve settings). Optimization uses at most 100 problems. Results are on held-out splits with three seeds.

The decisive checks are three. First, joint search must beat the greedy per-round ablation at an equal budget. Second, the best single-shot and best loop generation prompts must swap rank on post-repair accuracy. Third, the optimized loop must beat compute-matched best-of-$n$. If prompt optimization on program-form GSM8K improves over the generic loop, it would also show that the GSM8K failure reported for prompt-side optimization is specific to tasks where the model must reason internally rather than through an executor.