-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexternal_action_runtime.py
More file actions
294 lines (265 loc) · 11.3 KB
/
Copy pathexternal_action_runtime.py
File metadata and controls
294 lines (265 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
from __future__ import annotations
from typing import Any, Literal
from pydantic import Field, ValidationError
from agent.contracts import RuntimeExecutionContext, RuntimeResponse
from runtime_service.dynamic_loop import FinishEvaluation
from runtime_service.planner import (
CallToolDecision,
FinishDecision,
InvalidPlannerDecisionError,
Planner,
PlannerContext,
ToolObservation,
)
from .dynamic_runtime import DynamicTravelRuntime, TravelFinishPayload
from .runtime import TravelMessageInput
from .state import AgentState, TravelPlan
from .tools.models import CreateTripHoldInput
from .validator import TravelValidator
class TravelExternalActionInput(TravelMessageInput):
"""Opt-in Travel 1.2 input; older Travel input schemas remain unchanged."""
requested_action: Literal["plan_only", "create_hold"] = Field(
default="plan_only"
)
class DurableActionTravelPlanner:
"""Add one explicit Travel action after the base Planner has enough evidence."""
ACTION_TOOL_NAME = "create_trip_hold"
def __init__(self, base_planner: Planner) -> None:
self.base_planner = base_planner
def decide(self, context: PlannerContext):
# The base/model Planner is plan-only: it never sees the write tool.
# This wrapper alone may insert the action after deterministic FINISH
# validation, so even a compromised or drifting provider cannot select
# an external write early or during a plan-only run.
base_context = context.model_copy(
update={
"tools": [
descriptor
for descriptor in context.tools
if descriptor.name != self.ACTION_TOOL_NAME
]
},
deep=True,
)
base_decision = self.base_planner.decide(base_context)
if (
isinstance(base_decision, CallToolDecision)
and base_decision.tool_name == self.ACTION_TOOL_NAME
):
raise InvalidPlannerDecisionError(
"The base Travel Planner cannot select external actions"
)
requested_action = context.runtime_input.get("requested_action", "plan_only")
if requested_action == "plan_only":
return base_decision
if requested_action != "create_hold":
raise InvalidPlannerDecisionError("Unsupported Travel external action")
if not isinstance(base_decision, FinishDecision):
return base_decision
by_tool = {observation.tool_name: observation for observation in context.observations}
existing_hold = by_tool.get(self.ACTION_TOOL_NAME)
expected_arguments = self._hold_arguments(
base_decision,
by_tool,
AgentState.model_validate(context.state),
)
if existing_hold is None:
return CallToolDecision(
tool_name=self.ACTION_TOOL_NAME,
arguments=expected_arguments,
reason="explicit_hold_requested_after_validated_plan_evidence",
)
self._validate_hold_observation(existing_hold, expected_arguments)
return base_decision
@staticmethod
def _hold_arguments(
base_decision: FinishDecision,
by_tool: dict[str, ToolObservation],
state: AgentState,
) -> dict[str, Any]:
try:
finish_payload = TravelFinishPayload.model_validate(base_decision.output)
except ValidationError:
raise InvalidPlannerDecisionError(
"Trip hold base finish payload is invalid"
) from None
if finish_payload.destination != state.destination:
raise InvalidPlannerDecisionError(
"Trip hold destination does not match requested destination"
)
if finish_payload.days != state.days:
raise InvalidPlannerDecisionError(
"Trip hold day count does not match requested day count"
)
try:
search = by_tool["search_trip_options"]
ranking = by_tool["rank_trip_options"]
route = by_tool["route_cost_summary"]
except KeyError as exc:
raise InvalidPlannerDecisionError(
"Trip hold requires search, ranking, and cost evidence"
) from exc
ranking_values = ranking.result.get("ranking")
if not isinstance(ranking_values, list) or not ranking_values:
raise InvalidPlannerDecisionError("Trip hold ranking evidence has no winner")
winner = ranking_values[0]
if not isinstance(winner, dict) or not isinstance(winner.get("name"), str):
raise InvalidPlannerDecisionError("Trip hold ranking winner is invalid")
selected_option_name = winner["name"]
if finish_payload.selected_option_name != selected_option_name:
raise InvalidPlannerDecisionError(
"Trip hold selection does not match the base Planner finish"
)
destination = search.result.get("destination")
if not isinstance(destination, str) or not destination:
raise InvalidPlannerDecisionError("Trip hold search destination is invalid")
if destination != state.destination:
raise InvalidPlannerDecisionError(
"Trip hold search destination does not match requested destination"
)
options = search.result.get("options")
if not isinstance(options, list):
raise InvalidPlannerDecisionError("Trip hold search evidence has no options")
selected = next(
(
option
for option in options
if isinstance(option, dict)
and option.get("name") == selected_option_name
),
None,
)
if selected is None:
raise InvalidPlannerDecisionError(
"Trip hold selection is absent from search evidence"
)
quoted_total = route.result.get("total_cost")
if not isinstance(quoted_total, int) or isinstance(quoted_total, bool):
raise InvalidPlannerDecisionError("Trip hold quoted total is invalid")
if route.result.get("within_budget") is not True:
raise InvalidPlannerDecisionError(
"Trip hold cannot be created for an over-budget option"
)
selected_total = sum(
DurableActionTravelPlanner._required_cost(selected, field_name)
for field_name in ("transport_cost", "hotel_cost", "activity_cost")
)
if quoted_total != selected_total:
raise InvalidPlannerDecisionError(
"Trip hold quoted total does not match search evidence"
)
if route.result.get("budget") != state.budget:
raise InvalidPlannerDecisionError(
"Trip hold cost evidence does not match requested budget"
)
itinerary = TravelPlan(
destination=state.destination or finish_payload.destination,
days=state.days or finish_payload.days,
flight_type=str(selected.get("flight_type", "unknown")),
hotel_tier=str(selected.get("hotel_tier", "unknown")),
poi_style=str(selected.get("poi_style", "unknown")),
total_cost=selected_total,
notes=[],
)
candidate = state.model_copy(
update={
"itinerary": itinerary,
"blockers": [],
"current_stage": "planned",
},
deep=True,
)
validation_errors = TravelValidator().validate(candidate).errors
if validation_errors:
raise InvalidPlannerDecisionError(
"Trip hold candidate failed deterministic Travel validation"
)
return CreateTripHoldInput(
destination=destination,
selected_option_name=selected_option_name,
quoted_total=quoted_total,
).model_dump(mode="json")
@staticmethod
def _required_cost(option: dict[str, Any], field_name: str) -> int:
value = option.get(field_name)
if not isinstance(value, int) or isinstance(value, bool):
raise InvalidPlannerDecisionError(
f"Trip hold option has invalid {field_name}"
)
return value
@staticmethod
def _validate_hold_observation(
observation: ToolObservation,
expected_arguments: dict[str, Any],
) -> None:
try:
normalized = CreateTripHoldInput.model_validate(
observation.arguments
).model_dump(mode="json")
except ValidationError:
raise InvalidPlannerDecisionError(
"Trip hold observation arguments are invalid"
) from None
if normalized != expected_arguments:
raise InvalidPlannerDecisionError(
"Trip hold observation does not match the selected option"
)
provider_reference = observation.result.get("provider_reference")
if observation.result.get("status") != "held" or not isinstance(
provider_reference, str
) or not provider_reference:
raise InvalidPlannerDecisionError("Trip hold observation is not successful")
for key, value in expected_arguments.items():
if observation.result.get(key) != value:
raise InvalidPlannerDecisionError(
"Trip hold result does not match its normalized arguments"
)
class DurableActionTravelRuntime(DynamicTravelRuntime):
"""Travel 1.2 adapter that validates the explicitly requested external action."""
def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
self._requested_action: Literal["plan_only", "create_hold"] = "plan_only"
def execute(
self,
state: AgentState,
runtime_input: TravelExternalActionInput,
context: RuntimeExecutionContext,
) -> RuntimeResponse[AgentState]:
self._requested_action = runtime_input.requested_action
try:
return super().execute(state, runtime_input, context)
finally:
self._requested_action = "plan_only"
def _evaluate_finish(
self,
state: AgentState,
decision: FinishDecision,
observations: list[ToolObservation],
) -> FinishEvaluation:
evaluation = super()._evaluate_finish(state, decision, observations)
if self._requested_action == "plan_only":
return evaluation
hold = self._observation(observations, "create_trip_hold")
expected_arguments = DurableActionTravelPlanner._hold_arguments(
decision,
{observation.tool_name: observation for observation in observations},
state,
)
DurableActionTravelPlanner._validate_hold_observation(
hold,
expected_arguments,
)
provider_reference = str(hold.result["provider_reference"])
return evaluation.model_copy(
update={
"output": {
**evaluation.output,
"external_action": {
"tool_name": "create_trip_hold",
"status": "held",
"provider_reference": provider_reference,
},
}
},
deep=True,
)