Solving the Core Challenges of Long-Horizon Agent Reinforcement Learning

    This article examines why long-horizon agents become overly conservative under outcome-only reinforcement learning and presents two technical approaches to address the problem: adaptive optimization with process-level credit assignment through layered reward structures and verifiable signals, and scaling sandboxed execution environments with strong isolation, snapshot rollback, deterministic builds, observability, and elastic scheduling. It includes a sample reward computation function and practical guidance for building reliable training and evaluation infrastructure.

    1. The Starting Problem: Why Long-Horizon Agents Become More Conservative Over Time

    In real software engineering and automated operations scenarios, an agent rarely completes a task in a single question-and-answer exchange. Instead, it must explore, locate, modify, and verify a solution across dozens or even hundreds of interaction turns. Take bug fixing as an example: the model first reads the repository structure, understands module dependencies, reproduces the issue, forms a hypothesis, modifies the code, and then runs tests to confirm the result. A small mistake at any step in this chain can cause the final verification to fail.

    This creates the most difficult signal problem in reinforcement learning: outcomes are sparse and binary. If a scalar reward of “success or failure” is provided only at the end of a trajectory, a single failure indiscriminately penalizes all preceding steps, even though most of the decisions may have been correct. This is similar to how board games are evaluated—losing the final game does not mean every move was bad, but purely outcome-driven optimization cannot distinguish a brilliant move from a careless one.

    Training this way over time leads the policy toward two forms of degradation. First, it becomes overly conservative, reduces exploration, and uses longer contexts and more turns to “confirm” its actions, driving up inference costs. Second, it misattributes failure and suppresses effective behaviors along with ineffective ones, resulting in the phenomenon of getting weaker the more it is trained. The core tension in long-horizon agent training can therefore be summarized in one sentence: without process signals, learning is inefficient, yet process signals are themselves difficult to obtain automatically.

    2. Path One: Adaptive Optimization and Process-Level Credit Assignment

    The first layer of the solution is to transform “outcome-only evaluation” into “layered evaluation.” A common industry approach can be abstracted into a three-tier reward structure:

    1. Outcome-level reward: whether the task genuinely passes verification, such as all unit tests, integration tests, or policy checks passing.
    2. Stage-level reward: whether verifiable intermediate milestones are completed, such as successfully reproducing the issue, producing a runnable patch, or passing static checks.
    3. Step-level reward: whether a single action is compliant and produces effective information gain, such as reading the correct file or avoiding destructive operations.

    The reward combination logic can be expressed schematically as:

    1. def compute_reward(traj):
    2. r_result = 1.0 if traj.all_checks_passed else 0.0
    3. r_stage = 0.3 * len(traj.passed_milestones) / max(1, len(traj.milestones))
    4. r_step = 0.1 * traj.valid_action_ratio
    5. penalty = 0.2 * traj.unsafe_action_count
    6. return r_result + r_stage + r_step - penalty

    The key point is that stage and step rewards must be produced by automatically verifiable signals, not assigned arbitrarily by another subjective model. Otherwise, noise and a “grader circular dependency” are introduced: if the model does not improve, scoring is inaccurate, and if scoring is inaccurate, the model finds it even harder to improve. Viable automated sources include whether compilation succeeds, whether static analysis raises alerts, changes in test coverage, command exit codes, and abnormal resource consumption. These signals are objective, inexpensive, and scalable, forming a reliable foundation for process rewards.

    In addition, a counterfactual baseline can be introduced: sample multiple trajectories for the same task and use within-group relative advantage instead of absolute scores, reducing the impact of noise from a single trajectory on the gradient. Mathematically, this is equivalent to telling the model “in the same situation, this step of yours was better or worse than average,” which is far more precise than “you ultimately failed, so everything was wrong.”

    3. Path Two: Scaling Sandboxes and Trusted Execution Environments

    The second path treats the training environment itself as a scalable resource. Long-horizon tasks require a large number of parallel executable environments to generate sufficient trajectory coverage. Increasing the number and diversity of environments essentially expands the state space the agent can access, which can be viewed as a form of “scaling on the environment side.”

    In engineering terms, a trusted execution environment needs at least the following capabilities:

    • Strong isolation: each task runs in an independent container with restricted filesystem and network policies to avoid cross-contamination.
    • Snapshot and rollback: the ability to restore the initial state after failure, supporting branch exploration and retries.
    • Deterministic builds: locked dependency versions to ensure the same task produces consistent results at different times, so rewards remain comparable.
    • Fine-grained observability: complete records of commands, exit codes, elapsed time, and peak resource usage, providing raw material for process rewards and failure attribution.
    • Elastic scheduling: the ability to handle thousands of concurrent environments, requiring queues, image pre-warming, resource quotas, and timeout reclamation mechanisms.

    When deployed in the cloud, these are typically combined by using a container platform for isolated execution, object storage for saving images and artifacts, a message queue for task distribution, and logging services with monitoring and alerting for tracking and anomaly detection. The general-purpose capability categories offered by Baidu AI Cloud in these scenarios can serve as a reference foundation for building elastic training and evaluation infrastructure.

    It is important to emphasize that scaling the environment is not about blindly adding machines. If the task set itself is too narrow, expansion