有界迭代预算的 for 循环
唯一一个用固定迭代上限的实现:max_iterations 默认 5,循环体是 range 而不是 while。每轮开头先消费 force_finish(由 rails 在 AFTER_REACT_ITERATION 设置),让优雅中止落在迭代边界而不是打断执行到一半的一轮;随后注入 rails 放行的 steering 消息。真正的长任务由外层 Task Loop 承担,ReAct 只负责「一步做对」。
for iteration in range(start_iteration, self._config.max_iterations):
logger.info(f"ReAct iteration {iteration + 1}/{self._config.max_iterations}")
# Honor force_finish requests set at iteration boundary
# (e.g. by rails on AFTER_REACT_ITERATION). This lets a
# graceful abort take effect at the top of the next
# iteration, after the previous one fully completes.
boundary_finish = ctx.consume_force_finish()
if boundary_finish:
await self.context_engine.save_contexts(session)
invoke_inputs.result = boundary_finish.result
break
# Inject the steering messages the rails let through
# before the next model call.
steering = await self._drain_steering_batch(ctx)