arXiv:2610.19184cs.CLOct 2026

Manuscript Offprint

arXiv:2610.19184

HERO: Harnessing Evidence for Test-time Refinement and Prompt Optimization in Program Generation

Mingyu Lu, Timothy Ossowski, Tristan Naumann, Hoifung Poon, Peniel Argaw

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Abstract

Harness optimization improves LLMs by refining reusable prompts and scaffolding, yet requires further adaptation for program generation tasks such as text-to-SQL and text-to-Python. Existing methods often rely on LLM reflectors to propose harness updates, which can fix observed failures but also disrupt previously correct behavior. Text-to-code tasks offer a natural way to mitigate this issue: we can execute generated outputs, allowing us to evaluate and verify the effects of proposed updates directly. Building on this signal, we introduce HERO (Harnessing Evidence for Refinement and Optimization), which combines offline and test-time harness optimization. At the first stage, HERO learns reusable prompt updates from execution-verified successes and failures and validates candidate revisions before adoption. At test time, we treat the generated code as an instance-specific harness and refine it using execution feedback. Across four SQL, Cypher, and Python benchmarks, HERO achieves the best offline performance, improving pass@1 by 7.6 points over the initial prompt, while test-time refinement adds another 2.0 points and improves execution efficiency.

1 Introduction

LLM agents are increasingly employed to interact with external environments through tools, often by generating executable programs such as SQL and Cypher queries and Python functions (Li et al., 2023; Schick et al., 2023; Feng et al., 2025; Zhuo et al., 2025; Ouyang et al., 2026). These artifacts provide a flexible interface for models to perform computation and interact with external systems (Gao et al., 2023; Wang et al., 2024). Their quality depends not only on the underlying model, but also on the surrounding harness—the prompts, tools, and memory that shape generation (Khattab et al., 2024; Lee et al., 2026; Li et al., 2026; Lopopolo, 2026). This makes the harness a natural target for improving LLM agent performance. Accordingly, existing work has pursued two related directions: optimizing reusable harness components across tasks (Lee et al., 2026; Lin et al., 2026; Yang et al., 2026; Agrawal et al., 2026), and using test-time feedback to select or refine generated outputs (Ni et al., 2023; Chen et al., 2024; Madaan et al., 2023).

Improving program generation through these directions presents two linked challenges. First, reusable updates must correct recurring errors without degrading behavior that already works. LLM-generated feedback can be unreliable without external grounding (Huang et al., 2024; Kamoi et al., 2024), and prompt revisions that resolve failures can introduce regressions on previously successful cases (Wu et al., 2024b). The challenge is therefore to derive grounded diagnoses of what to change and preserve from execution outcomes, and translate these diagnoses into reusable guidance. Second, this general guidance must be complemented by feedback specific to the current instance. An optimized prompt captures strategies learned across training tasks, but a generated program may still encounter errors that require observations from its own execution. Approaches such as Dynamic Cheatsheet (DC) and ACE accumulate experience through evolving shared contexts (Suzgun et al., 2026; Zhang et al., 2026). When shared context is updated at test time, unreliable or overly specific feedback can become persistent guidance, allowing errors from one instance to affect subsequent tasks. Together, these challenges mo-

*Work done during an internship at Microsoft Research.

tivate grounding offline harness updates in execution evidence while keeping test-time corrections local to the current program.

To address these challenges, we introduce HERO (Harnessing Evidence for Refinement and Optimization), a two-level framework that grounds reusable guidance updates in execution evidence and confines test-time adaptation to the current program. Our contributions are threefold:

• A unified two-level formulation. We formulate program generation as a two-level optimization problem that couples offline prompt optimization with test-time refinement of instance-specific programs.

• Execution-grounded feedback and verification. Offline, we use execution evidence from successful and failed programs under prior prompts to identify what to correct and preserve. At test time, execution feedback and retrieved verified training examples guide program revisions, which are accepted only when supported by evidence.

• Empirical evaluation and analysis. We demonstrate correctness gains over existing offline harness optimizers across diverse query-generation and programming tasks, with further improvements from test-time refinement. We further evaluate execution efficiency and computational cost, finding that the learned prompts remain compact across tasks.