Agent: : Update ToolRunContext and Execution Hooks for PlanV2 Nativity #815
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Original Task
Goal
To align the context objects exposed to tools and execution hooks with the new
PlanV2-nativeRunContext. This ensures that all parts of the system, including user-defined tools and hooks, operate on the new, consistent data structures.Implementation Details
ToolRunContext: Refactor theToolRunContextclass. It should no longer contain direct references to the legacyPlanorPlanRun. Instead, it should provide access to the necessary execution data via the newPlanRunV2object held withinRunContext.ExecutionHooks. For example,on_step_start(step: Step)should be updated toon_step_start(step: StepV2). The hooks should receive the modernStepV2objects and the newPlanRunV2context.ToolRunContextto use the new V2-native fields.Acceptance Criteria
ToolRunContextno longer exposes legacyPlanorPlanRunobjects.StepV2andPlanRunV2.ToolRunContext.Additional Context
Based on the analysis of the provided context, here is a summary of relevant information for the coding agent:
1. Architectural Context & Data Structures
The core of this change is to adopt a new
PlanRunV2object which will be held within the mainRunContext. The legacyPlanRunclass will be removed.The target data structures, as defined in the "Plan Builder V2 Migration" document, should be:
2.
ToolRunContextRefactoringToolRunContextclass provides direct access to legacy objects:plan: Planandplan_run: PlanRun.ToolRunContextto remove these legacy fields. Instead, it should provide access to the necessary execution data via the newplan_run: PlanRunV2object held within the mainRunContext.get_tool_run_ctx()method onRunContext, which is likely the point of instantiation forToolRunContextand will need to be updated.3.
ExecutionHooksSignature UpdatesExecutionHookscurrently use legacy V1 objects (e.g.,Step).on_step_start(step: Step)should becomeon_step_start(step: StepV2).PlanRunV2context where applicable.Agent Logs
Summary
This implementation successfully migrates the
ToolRunContextandExecutionHooksto be fully PlanV2-native, aligning with the new execution model. The changes eliminate dependencies on legacyPlanandPlanRunobjects, replacing them with the modernPlanV2andPlanRunV2structures.Files Modified
portia/plan_run.py
StepOutputValueclass to represent step outputs with metadataPlanRunV2class as the V2-native plan run representationget_outstanding_clarifications(),get_clarifications_for_step(),get_clarification_for_step()portia/run_context.py
PlanRunV2as the core data structurePlan,PlanRun, and redundant fieldsget_tool_run_ctx()to create ToolRunContext with PlanRunV2portia/tool.py
ToolRunContextto usePlanRunV2instead of legacy structuresend_user,plan, andconfigfieldsplan_runfor backwards compatibility:end_userproperty returnsplan_run.end_userconfigproperty returnsplan_run.configplanproperty returnsplan_run.plan(PlanV2)PlanRunV2andPlanV2portia/execution_hooks.py
before_step_execution: Now usesCallable[[PlanV2, PlanRunV2, StepV2], ...]after_step_execution: Now usesCallable[[PlanV2, PlanRunV2, StepV2, Output], ...]before_plan_run: Now usesCallable[[PlanV2, PlanRunV2], ...]after_plan_run: Now usesCallable[[PlanV2, PlanRunV2, Output], ...]before_tool_call: Now usesCallable[[Tool, dict, PlanRunV2, StepV2], ...]after_tool_call: Now usesCallable[[Tool, Any, PlanRunV2, StepV2], ...]clarify_on_all_tool_calls,clarify_on_tool_calls,log_step_outputs) to use new signaturesportia/open_source_tools/browser_tool.py
step_complete()method to work with PlanV2 and StepV2step.tool_idon legacy Steps to checkingInvokeToolStepinstancesportia/init.py
PlanRunV2andStepOutputValue__all__list to include new classesKey Features Added
1. PlanRunV2 Class
2. Simplified ToolRunContext
PlanRunV2end_user,config, andplan3. V2-Native ExecutionHooks
4. Internal Tool Updates
Testing
Manual Verification Performed:
portia/__init__.pyKey Compatibility Considerations:
end_user,config, andplanproperties ensures existing tool code that accesses these fields continues to worktool_iddirectly, maintaining functionality while supporting the new modelAreas for Future Testing: