-
Notifications
You must be signed in to change notification settings - Fork 97
Agent: agent/task-20250926-081342 #810
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,7 @@ | |
| from portia.end_user import EndUser | ||
| from portia.execution_hooks import ExecutionHooks | ||
| from portia.plan import Plan | ||
| from portia.plan_run import PlanRun | ||
| from portia.plan_run import PlanRun, PlanRunV2 | ||
| from portia.storage import Storage | ||
| from portia.telemetry.telemetry_service import BaseProductTelemetry | ||
| from portia.tool import ToolRunContext | ||
|
|
@@ -52,3 +52,116 @@ def get_tool_run_ctx(self) -> ToolRunContext: | |
| config=self.config, | ||
| clarifications=self.plan_run.get_clarifications_for_step(), | ||
| ) | ||
|
|
||
|
|
||
| class RunContextV2(BaseModel): | ||
| """Updated context for PlanV2 runs using consolidated data structures. | ||
|
|
||
| This is the V2 version of RunContext that uses the new consolidated | ||
| PlanRunV2 structure instead of separate plan/legacy_plan/plan_run fields. | ||
| """ | ||
|
|
||
| model_config = ConfigDict(arbitrary_types_allowed=True) | ||
|
|
||
| plan_run: PlanRunV2 = Field(description="The consolidated plan run instance.") | ||
| storage: Storage = Field(description="The Portia storage.") | ||
| tool_registry: ToolRegistry = Field(description="The Portia tool registry.") | ||
| execution_hooks: ExecutionHooks = Field(description="The Portia execution hooks.") | ||
| telemetry: BaseProductTelemetry = Field(description="The Portia telemetry service.") | ||
|
|
||
| def get_tool_run_ctx(self) -> ToolRunContext: | ||
| """Get the tool run context for V2.""" | ||
| # Create a legacy plan if needed for backwards compatibility | ||
| legacy_plan = None | ||
| if self.plan_run.plan: | ||
| from portia.plan import PlanContext | ||
| # Create a minimal plan context for legacy compatibility | ||
| plan_context = PlanContext(query="", tool_registry=self.tool_registry) | ||
| legacy_plan = self.plan_run.plan.to_legacy_plan(plan_context) | ||
|
|
||
| # Create a legacy PlanRun for ToolRunContext compatibility | ||
| legacy_plan_run = PlanRun( | ||
| id=self.plan_run.id, | ||
| plan_id=self.plan_run.plan.id if self.plan_run.plan else "unknown", | ||
| current_step_index=self.plan_run.current_step_index, | ||
| state=self.plan_run.state, | ||
| end_user_id=self.plan_run.end_user.external_id, | ||
| outputs=self.plan_run._legacy_outputs, | ||
| plan_run_inputs=self.plan_run.plan_run_inputs, | ||
| ) | ||
|
|
||
| return ToolRunContext( | ||
| end_user=self.plan_run.end_user, | ||
| plan_run=legacy_plan_run, | ||
| plan=legacy_plan, | ||
| config=self.plan_run.config, | ||
| clarifications=self.plan_run.get_clarifications_for_step(), | ||
| ) | ||
|
|
||
|
|
||
| # Migration helpers for RunContext structures | ||
| def migrate_run_context_to_v2( | ||
| legacy_context: RunContext, | ||
| ) -> RunContextV2: | ||
| """Convert a legacy RunContext to RunContextV2. | ||
|
|
||
| Args: | ||
| legacy_context: The legacy RunContext instance to convert. | ||
|
|
||
| Returns: | ||
| RunContextV2: The converted RunContextV2 instance. | ||
| """ | ||
| from portia.plan_run import migrate_plan_run_to_v2 | ||
|
|
||
| # Convert RunContext to RunContextV2 by migrating the PlanRun | ||
| plan_run_v2 = migrate_plan_run_to_v2( | ||
| legacy_plan_run=legacy_context.plan_run, | ||
| plan_v2=legacy_context.plan, | ||
| end_user=legacy_context.end_user, | ||
| config=legacy_context.config, | ||
| ) | ||
|
|
||
| return RunContextV2( | ||
| plan_run=plan_run_v2, | ||
| storage=legacy_context.storage, | ||
| tool_registry=legacy_context.tool_registry, | ||
| execution_hooks=legacy_context.execution_hooks, | ||
| telemetry=legacy_context.telemetry, | ||
| ) | ||
|
|
||
|
|
||
| def migrate_v2_to_run_context( | ||
| context_v2: RunContextV2, | ||
| ) -> RunContext: | ||
| """Convert a RunContextV2 back to legacy RunContext for compatibility. | ||
|
|
||
| Args: | ||
| context_v2: The RunContextV2 instance to convert. | ||
|
|
||
| Returns: | ||
| RunContext: The converted legacy RunContext instance. | ||
| """ | ||
| from portia.plan_run import migrate_v2_to_plan_run | ||
|
|
||
| # Convert back to legacy structures | ||
| legacy_plan_run = migrate_v2_to_plan_run(context_v2.plan_run) | ||
|
|
||
| # Create legacy plan if available | ||
| legacy_plan = None | ||
| if context_v2.plan_run.plan: | ||
| from portia.plan import PlanContext | ||
| plan_context = PlanContext(query="", tool_registry=context_v2.tool_registry) | ||
| legacy_plan = context_v2.plan_run.plan.to_legacy_plan(plan_context) | ||
|
|
||
| return RunContext( | ||
| plan=context_v2.plan_run.plan if context_v2.plan_run.plan else None, | ||
| legacy_plan=legacy_plan, | ||
| plan_run=legacy_plan_run, | ||
| end_user=context_v2.plan_run.end_user, | ||
| step_output_values=context_v2.plan_run.step_output_values, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: RunContext Validation Fails Due to Type MismatchesThe |
||
| config=context_v2.plan_run.config, | ||
| storage=context_v2.storage, | ||
| tool_registry=context_v2.tool_registry, | ||
| execution_hooks=context_v2.execution_hooks, | ||
| telemetry=context_v2.telemetry, | ||
| ) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: PlanRun Constructor Receives Incorrect Type
The
PlanRunconstructor'splan_idfield expects aPlanUUID. However, inRunContextV2.get_tool_run_ctxandmigrate_v2_to_plan_run, it's passed the string "unknown" when thePlanV2object isNone. This type mismatch could lead to validation errors.Additional Locations (1)
portia/plan_run.py#L416-L420