Dissecting AI
From the paper: TreeFlex: Agents Navigating Their Own Search Trees

TreeFlex: Tree-Aware Policy Optimization

A search tree generates outputs from multiple restored contexts, so TreeFlex treats each complete search as one group-relative rollout and trains each token only in the causal context where it was produced.

Read the paper ↗
The short version

What it is

  • Complete-search rollout One full tree search is treated as a single learning example, even though it contains several separate sequences.
  • Causal block A block is a stretch of generated text whose context only grows by adding more text; restored contexts start a new block.
  • Loss mask Training applies only to text the model generated in that block, not saved context, tool results, or copied instructions and cards.

Why it matters

This lets one task outcome teach the policy about the whole search while avoiding training a token as if it had been generated from a context it never saw. The averaging choice then decides whether long searches, many segments, or different roles receive more influence.

Before we get into the mechanics, here is the short version: a tree search can contain several stretches of model output, each started from a different saved context. TreeFlex treats the entire search as one learning example, but trains each stretch only against the context that actually produced it. That lets a final task outcome speak to the whole search without pretending it was one uninterrupted conversation.

  1. The sample is the whole search: In ordinary agentic training, it is natural to call one growing conversation a rollout. Here that breaks down because Reflection can choose a saved node and Execution resumes from the state stored there. The canonical example is a research search that gathers evidence in several segments and submits one answer at the end. The final answer determines the task result, so the complete search—not each segment—is the unit compared against other searches for that task. A process penalty, such as a rejected tool call, is narrower: it concerns the turn where it happened. What keeps the token-level training honest when the search branches and resumes?

  2. A saved context is input, not fresh output: Think of each stretch of text as a notebook page whose prefix only grows by additions. When a segment resumes an older saved page, that is a new stretch for training purposes: the restored page is context, while the newly written text is the output. The analogy is useful for remembering what gets trained, though a model context is not literally a notebook. A concrete case is a checkpoint card written by Execution and later read by Reflection. Train the card where Execution wrote it; do not train it again as though Reflection had generated it. This is what the loss mask does: it keeps newly generated text and excludes prompts, restored context, harness messages, and tool observations. So once the right tokens are selected, what determines how much each kind of text matters?

  3. The averaging rule is a credit-allocation choice: You can give equal weight to every token, every causal block, or every complete search. Token averaging naturally gives longer outputs more total influence. Block averaging gives a short segment as much weight as a long segment. Rollout averaging first totals the contribution within each search, then gives each search equal weight; the described setup uses this choice. There is also a role-level option: because Execution can produce much more text than Reflection, mixing in a role average can raise Reflection’s share. These are not merely different ways to write the same loss; they answer different questions about what should count equally. Which behavior do you want the policy to prioritize: volume, segments, whole searches, or the decisions of both roles?

The task score supplies the search-level signal, while process penalties attach to particular turns. In the cited settings, task success is judged for the submitted research answer or repository state; penalties cover specified process failures rather than rewarding a preferred tree shape. I find the separation useful: it avoids confusing “this search solved the task” with “every turn was equally good.”

Read the deep dive

Why People Wanted It

In ordinary agentic reinforcement learning, a rollout is one growing token sequence. A tree search does not fit that picture: Reflection can resume a saved node, and each Execution segment starts from the context saved at its selected node. The same search therefore contains several sequences with different histories, yet only its complete outcome determines whether the task succeeded.

So the real question was never just how to score an answer. It was how to treat the whole search as one learning sample without pretending that every generated token came from one uninterrupted context.

The Core Move

Treat each complete search as one group-relative rollout, but compute each token’s learning signal in the causal block and context where that token was generated.

The source gives the reward and loss structure in prose and named quantities, but does not provide a complete displayed objective formula in the cited pages. The essential distinction is: a task reward belongs to the search; a process penalty belongs only to the turn it concerns; and restored context is input, not newly generated output to train.

Quiz
In TreeFlex, what counts as one member of a group-relative policy-optimization group?
Each generated token
Each complete search for the task
Each individual Execution segment
Each checkpoint card

Why One Complete Search Is the Rollout

Now the version for readers who want more of the machinery: group-relative comparison happens between complete searches for the same task, not between isolated segments. The old policy samples KK complete searches, each with a terminal score RiR_i; the group mean and standard deviation are computed from those terminal scores alone. In words, the baseline compares task outcomes without letting a local process penalty shift the baseline for other turns.

A canonical example is a research search that issues several instructions, performs multiple retrieval segments, then submits one answer. That answer receives the terminal score for the whole search, and the score applies to every generated output in it. A malformed tool call, by contrast, is a turn-level event: its penalty applies only to tokens in that turn. If all searches in the group have the same score, the normalization divisor is one, preserving penalties at their original scale. Without penalties, every generated token in a search has the same advantage.

The task score is binary in the described settings: a deep-research answer scores one if the precision-first judge accepts it, and a SWE search scores one if the official tests mark the task resolved; other outcomes score zero. Process signals are non-positive penalties. A malformed or rejected tool call receives Q=−1Q=-1; in deep research, judged out-of-scope work can receive Q=−0.2Q=-0.2. In words, these signals discourage specific turns without inventing a separate bonus for choosing a tree shape or checkpointing. What should count as a fair comparison when searches produce very different amounts of text?

Why Restored Context Creates Separate Causal Blocks

Here is the part that gets technical: a causal block is a maximal sequence in which context grows only by appending tokens, preserving the prefix from which every token was sampled. Reflection is one block because its context grows throughout the search; each Execution segment is its own block because the next segment may begin from restored context.

For a token at rollout ii, block bb, and position tt, the notation zi,b,tz_{i,b,t} names the generated token, hi,b,th_{i,b,t} its generating context, and mi,b,t∈{0,1}m_{i,b,t}\in\{0,1\} its loss mask. In words, the mask is one for tokens generated by the model in that block and zero for the prompt, restored context, harness messages, and tool observations. Instructions and summary cards copied into the other role’s context are masked there too: the instruction is trained when Reflection writes it, and the card when Execution writes it, not again when the other role reads it.

Take an Execution segment that writes a checkpoint card, followed by a later segment resumed from that checkpoint. The later segment can use the restored state as context, but the earlier segment’s tokens are not newly generated there and do not receive a second training loss. This is why each token’s log-probability is evaluated under the context from which it was generated. What happens when a search has many blocks, or Execution contributes far more tokens than Reflection?

Why Loss Aggregation Changes Which Decisions Count

Now the deeper accounting choice: PPO’s clipped surrogate is applied to generated tokens, with each token’s importance ratio evaluated under its own generating context. The weights determine whether equal influence belongs to tokens, blocks, complete rollouts, or roles; they do not change which tokens are trainable.

A token mean gives every token equal weight, so a long search contributes more. A block mean averages within each block and then across blocks, so a short Execution segment can count as much as a long one, and searches with more blocks contribute more. A rollout mean averages within each complete search and then across searches, giving each search equal total weight regardless of length or block count. A role mean instead first averages within Reflection and Execution, then across roles; mixing it with a base average uses a weight λ∈[0,1]\lambda\in[0,1]. In words, increasing λ\lambda gives the comparatively sparse Reflection outputs more influence against Execution’s larger token volume.

The described training setup uses rollout mean with λ=0\lambda=0. The cited text also describes dual clipping for negative advantages with c=3c=3, but the pages do not give a complete readable displayed objective to reproduce here. The tradeoff is clear: choosing the averaging unit changes what the optimizer treats as equally important, even though the generated tokens and their causal masks stay the same. Which unit best matches what you want a search policy to learn?

Minimal Causal Code

This sketch makes the mask and context boundary visible. It is not a reproduction of the full PPO objective; the cited pages describe the clipped objective but do not provide a complete legible formula here.

# Each block stores only newly generated tokens and the context used to generate them.
for rollout in group:
    for block in rollout.blocks:
        for token, context, generated_by_model in block.steps:
            if not generated_by_model:
                continue  # prompt, restored context, harness output, tool observation

            log_prob = policy.log_prob(token, context)
            advantage = rollout.task_advantage
            if block.turn_has_process_penalty:
                advantage += block.process_penalty

            loss = clipped_surrogate(log_prob, block.old_log_prob(token), advantage)
            train(loss)

What You Gain, and What You Lose

You can assign task-level credit to the complete search while keeping token training faithful to the context that produced each output. Turn-specific penalties can target local failures without moving the group baseline, and the loss average makes explicit whether tokens, blocks, rollouts, or roles receive equal weight.

The cost is bookkeeping: you must preserve each block’s generating context, mask restored or externally supplied text, associate penalties with the right turn, and choose an aggregation rule. Different aggregation rules emphasize different behavior; they are not interchangeable shortcuts. TreeFlex’s described setup uses rollout averaging without role mixing, so it does not explicitly equalize Reflection and Execution contributions.

Quiz
A later Execution segment receives a restored context containing an earlier segment’s card. Which tokens receive training loss in the later segment?
The restored card and all later generated tokens
Only Reflection’s instructions
Every token in the search, recomputed under the latest context
Only tokens newly generated in the later segment’s causal block
Core Takeaway

Score the whole search, but train each token only where it was born.