Refactor: Improve logic and error handling in KAGIterativePipeline
This commit addresses several issues I identified during a detailed review of kag/solver/pipeline/kag_iterative_pipeline.py:
-
Correct
max_iterationDocstring: The docstring formax_iterationin__init__now correctly states(default: 5)to match the actual default value in the code. -
Improved Context Management: Tasks are now added to the
contextonly after their successful execution. Previously, tasks were added before execution, which could lead to an inaccurate context if an error occurred. The final task is added before breaking the loop. -
Clearer Termination on Max Iterations:
- I introduced a
MaxIterationsReachedErrorcustom exception. - The pipeline now tracks if it was terminated after the final step.
- If the
max_iterationlimit is reached without the final step being called, the pipeline raisesMaxIterationsReachedErrorinstead of proceeding to generate a potentially misleading answer.
- I introduced a
Design Note (Further Discussion Recommended):
A significant design concern remains regarding how KAGIterativePipeline processes tasks. I've observed that the pipeline currently only processes the first task in each iteration, discarding any others. This might be an underutilization of capabilities or a point of inefficiency. This behavior has not been changed but warrants further discussion and potential future refactoring if the pipeline is intended to handle more complex plans within a single iteration.