|
11 | 11 | from pydantic import GetCoreSchemaHandler |
12 | 12 | from pydantic_core import core_schema |
13 | 13 |
|
| 14 | +from ._tool_invocation import tool_output_identity |
14 | 15 | from .agent import Agent |
15 | 16 | from .agent_output import AgentOutputSchemaBase |
16 | 17 | from .exceptions import ( |
|
29 | 30 | ModelResponse, |
30 | 31 | RunItem, |
31 | 32 | ToolApprovalItem, |
| 33 | + ToolCallOutputItem, |
32 | 34 | TResponseInputItem, |
33 | 35 | ) |
34 | 36 | from .logger import log_tool_action_warning, logger |
|
46 | 48 | ProcessedResponse, |
47 | 49 | QueueCompleteSentinel, |
48 | 50 | ) |
49 | | -from .run_state import RunState |
| 51 | +from .run_state import RunState, _PendingSessionWrite |
50 | 52 | from .stream_events import StreamEvent |
51 | 53 | from .tool_guardrails import ToolInputGuardrailResult, ToolOutputGuardrailResult |
52 | 54 | from .tracing import Trace |
@@ -156,6 +158,9 @@ def _populate_state_from_result( |
156 | 158 | else: |
157 | 159 | state._generated_prompt_cache_key = getattr(result, "_generated_prompt_cache_key", None) |
158 | 160 | state._pending_input = copy.deepcopy(getattr(result, "_pending_input_for_state", [])) |
| 161 | + state._pending_session_write = copy.deepcopy( |
| 162 | + getattr(result, "_pending_session_write", None) |
| 163 | + ) |
159 | 164 | state._current_step = getattr(result, "_current_step_for_state", None) |
160 | 165 | state._reasoning_item_id_policy = getattr(result, "_reasoning_item_id_policy", None) |
161 | 166 |
|
@@ -289,6 +294,52 @@ def _input_items_for_result( |
289 | 294 | if not isinstance(model_input_items, list): |
290 | 295 | return session_items |
291 | 296 |
|
| 297 | + if isinstance(result, RunResultStreaming): |
| 298 | + # Execution checkpoints can contain accepted outputs that final guardrails |
| 299 | + # have not released. Keep them private in every public history view. |
| 300 | + from .run_internal.blocked_output import ( |
| 301 | + _current_response_boundary, |
| 302 | + _identity_sequence_start, |
| 303 | + ) |
| 304 | + |
| 305 | + processed = result._last_processed_response |
| 306 | + if processed is None and isinstance(result._state, RunState): |
| 307 | + processed = result._state._last_processed_response |
| 308 | + if processed is not None: |
| 309 | + legacy_public_outputs: set[tuple[str, str]] = set() |
| 310 | + start = _identity_sequence_start(model_input_items, processed.new_items) |
| 311 | + if start is None and isinstance(result._state, RunState): |
| 312 | + # Legacy first-turn checkpoints have no shared processed-item identities. |
| 313 | + # Prove their boundary against this result's captured history, since the |
| 314 | + # caller can resume the source RunState before inspecting an earlier result. |
| 315 | + boundary_state = copy.copy(result._state) |
| 316 | + boundary_state._generated_items = model_input_items |
| 317 | + boundary_state._session_items = result.new_items |
| 318 | + boundary_state._current_turn = result.current_turn |
| 319 | + boundary = _current_response_boundary((), processed, boundary_state) |
| 320 | + start = boundary.generated_start |
| 321 | + if start is None and boundary_state._terminal_unrecoverable: |
| 322 | + return session_items |
| 323 | + if start is not None and boundary.session_start is not None: |
| 324 | + public_start = boundary.session_start + len(boundary.processed_items) |
| 325 | + legacy_public_outputs = { |
| 326 | + identity |
| 327 | + for item in result.new_items[public_start:] |
| 328 | + if isinstance(item, ToolCallOutputItem) |
| 329 | + and (identity := tool_output_identity(item.raw_item)) is not None |
| 330 | + } |
| 331 | + if start is not None: |
| 332 | + output_start = start + len(processed.new_items) |
| 333 | + public_items = {id(item) for item in result.new_items} |
| 334 | + model_input_items = [ |
| 335 | + item |
| 336 | + for index, item in enumerate(model_input_items) |
| 337 | + if index < output_start |
| 338 | + or not isinstance(item, ToolCallOutputItem) |
| 339 | + or id(item) in public_items |
| 340 | + or tool_output_identity(item.raw_item) in legacy_public_outputs |
| 341 | + ] |
| 342 | + |
292 | 343 | # When the runner marks a divergence, generated_items already reflect the continuation input |
293 | 344 | # chosen for the next local run after applying handoff/input filtering. |
294 | 345 | return run_items_to_input_items(model_input_items, reasoning_item_id_policy) |
@@ -367,6 +418,10 @@ class RunResultBase(abc.ABC): |
367 | 418 | default_factory=list, init=False, repr=False |
368 | 419 | ) |
369 | 420 | """Pending input preserved when a non-streaming result is converted back to RunState.""" |
| 421 | + _pending_session_write: _PendingSessionWrite | None = field( |
| 422 | + default=None, init=False, repr=False |
| 423 | + ) |
| 424 | + """Held pending Session write preserved when a non-streaming result becomes a RunState.""" |
370 | 425 | _current_step_for_state: Any = field(default=None, init=False, repr=False) |
371 | 426 | """Current step preserved when a non-streaming result is converted back to RunState.""" |
372 | 427 |
|
@@ -1250,5 +1305,20 @@ def to_state(self) -> RunState[Any]: |
1250 | 1305 | previous_response_id=self._previous_response_id, |
1251 | 1306 | auto_previous_response_id=self._auto_previous_response_id, |
1252 | 1307 | ) |
| 1308 | + if ( |
| 1309 | + isinstance(self._state, RunState) |
| 1310 | + and self._last_processed_response is not None |
| 1311 | + and self._state._last_processed_response is self._last_processed_response |
| 1312 | + ): |
| 1313 | + # The checkpoint retains records withheld from the public result until guardrails pass. |
| 1314 | + error = self.run_loop_exception |
| 1315 | + if error is not None and _is_error_data_redacted(error): |
| 1316 | + state._tool_input_guardrail_results = list( |
| 1317 | + self._state._tool_input_guardrail_results |
| 1318 | + ) |
| 1319 | + if self._last_processed_response.tool_output_guardrail_result_start is not None: |
| 1320 | + state._tool_output_guardrail_results = list( |
| 1321 | + self._state._tool_output_guardrail_results |
| 1322 | + ) |
1253 | 1323 | _copy_pending_nested_agent_tool_states(state, self) |
1254 | 1324 | return state |
0 commit comments