-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Python: include checkpoint_id on AG-UI interrupt metadata (#8150) #8163
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
Open
lsmlhi_25 (FOWEPJF255)
wants to merge
5
commits into
microsoft:main
Choose a base branch
from
FOWEPJF255:fix/ag-ui-interrupt-checkpoint-8150
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5c44073
Python: include checkpoint_id on AG-UI interrupt metadata (#8150)
lhl-lhi 8e36bd4
Python: Prefer runner pause checkpoint over shared get_latest (#8150)
lhl-lhi c6f10d8
Python: Fix ag-ui lint and ty ignores for #8150 tests
FOWEPJF255 68f5abe
Python: Core resolve_pause_checkpoint_id for AG-UI (#8150)
FOWEPJF255 d4b60eb
fix(python): resolve pyright failures in pause checkpoint lookup
FOWEPJF255 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,7 @@ | |
| import json | ||
| import logging | ||
| import uuid | ||
| from collections.abc import AsyncGenerator | ||
| from collections.abc import AsyncGenerator, Awaitable, Callable | ||
| from functools import partial | ||
| from types import UnionType | ||
| from typing import Any, Union, cast, get_args, get_origin, get_type_hints | ||
|
|
@@ -127,7 +127,10 @@ def _workflow_interrupt_value(request_data: Any) -> Any: | |
| return {"data": safe_request_data} | ||
|
|
||
|
|
||
| def _workflow_interrupt_metadata(request_payload: dict[str, Any], value: Any) -> dict[str, Any]: | ||
| def _workflow_interrupt_metadata( | ||
| request_payload: dict[str, Any], | ||
| value: Any, | ||
| ) -> dict[str, Any]: | ||
| """Build Agent Framework metadata for workflow request_info interrupts.""" | ||
| agent_framework_metadata = { | ||
| key: make_json_safe(value) | ||
|
|
@@ -145,6 +148,104 @@ def _workflow_interrupt_metadata(request_payload: dict[str, Any], value: Any) -> | |
| return {"agent_framework": agent_framework_metadata} | ||
|
|
||
|
|
||
| def _attach_checkpoint_id_to_interrupts( | ||
| interrupts: list[dict[str, Any]], | ||
| checkpoint_id: str | None, | ||
| ) -> list[dict[str, Any]]: | ||
| """Attach ``checkpoint_id`` to each interrupt's ``metadata.agent_framework`` (issue #8150). | ||
|
|
||
| Multi-worker hosts need the pause checkpoint on the wire so the next resume can pass | ||
| ``forwardedProps.checkpoint_id`` without a side-channel lookup. No-op when checkpointing | ||
| is inactive or the id is already present. | ||
| """ | ||
| if not checkpoint_id or not interrupts: | ||
| return interrupts | ||
|
|
||
| attached: list[dict[str, Any]] = [] | ||
| for interrupt in interrupts: | ||
| entry = dict(interrupt) | ||
| metadata = entry.get("metadata") | ||
| if isinstance(metadata, dict): | ||
| metadata = dict(metadata) | ||
| else: | ||
| metadata = {} | ||
| agent_framework = metadata.get("agent_framework") | ||
| if isinstance(agent_framework, dict): | ||
| agent_framework = dict(agent_framework) | ||
| else: | ||
| agent_framework = {} | ||
| agent_framework.setdefault("checkpoint_id", checkpoint_id) | ||
| metadata["agent_framework"] = agent_framework | ||
| entry["metadata"] = metadata | ||
| attached.append(entry) | ||
| return attached | ||
|
|
||
|
|
||
| def _interrupt_request_ids(interrupts: list[dict[str, Any]]) -> set[str]: | ||
| return {str(item["id"]) for item in interrupts if item.get("id") is not None} | ||
|
|
||
|
|
||
| async def _pause_checkpoint_id_for_interrupts( | ||
| *, | ||
| workflow: Workflow, | ||
| checkpoint_storage: CheckpointStorage | None, | ||
| interrupts: list[dict[str, Any]], | ||
| known_checkpoint_id: str | None = None, | ||
| baseline_checkpoint_id: str | None = None, | ||
| ) -> str | None: | ||
| """Resolve the pause checkpoint for *this* run's interrupts via core. | ||
|
|
||
| ``baseline_checkpoint_id`` must be the runner id captured before ``workflow.run()`` so | ||
| a leftover id from a prior run is not advertised when this run did not persist. | ||
| """ | ||
| if not interrupts: | ||
| return None | ||
|
|
||
| resolve = getattr(workflow, "resolve_pause_checkpoint_id", None) | ||
| if not callable(resolve): | ||
| return None | ||
|
|
||
| # getattr returns a plain object to the type checker; cast to an awaitable callable. | ||
| resolve_fn = cast( | ||
| Callable[..., Awaitable[str | None]], | ||
| resolve, | ||
| ) | ||
| return await resolve_fn( | ||
| _interrupt_request_ids(interrupts), | ||
| checkpoint_storage=checkpoint_storage, | ||
| known_checkpoint_id=known_checkpoint_id, | ||
| baseline_checkpoint_id=baseline_checkpoint_id, | ||
| ) | ||
|
|
||
|
|
||
| async def _build_run_finished_with_checkpointed_interrupts( | ||
| *, | ||
| run_id: str, | ||
| thread_id: str, | ||
| interrupts: list[dict[str, Any]], | ||
| workflow: Workflow, | ||
| checkpoint_storage: CheckpointStorage | None, | ||
| known_checkpoint_id: str | None = None, | ||
| baseline_checkpoint_id: str | None = None, | ||
| ) -> Any: | ||
| """Build RUN_FINISHED, attaching the pause checkpoint id to interrupt metadata when available.""" | ||
| if not interrupts: | ||
| return _build_run_finished_event(run_id=run_id, thread_id=thread_id, interrupts=interrupts) | ||
|
|
||
| pause_checkpoint_id = await _pause_checkpoint_id_for_interrupts( | ||
| workflow=workflow, | ||
| checkpoint_storage=checkpoint_storage, | ||
| interrupts=interrupts, | ||
| known_checkpoint_id=known_checkpoint_id, | ||
| baseline_checkpoint_id=baseline_checkpoint_id, | ||
| ) | ||
| return _build_run_finished_event( | ||
| run_id=run_id, | ||
| thread_id=thread_id, | ||
| interrupts=_attach_checkpoint_id_to_interrupts(interrupts, pause_checkpoint_id), | ||
| ) | ||
|
|
||
|
|
||
| async def _pending_request_events(workflow: Workflow) -> dict[str, Any]: | ||
| """Best-effort retrieval of pending request_info events from workflow context.""" | ||
| runner_context = getattr(workflow, "_runner_context", None) | ||
|
|
@@ -1133,12 +1234,34 @@ async def run_workflow_stream( | |
| interrupt_event_value = _workflow_interrupt_event_value(request_payload) | ||
| if interrupt_event_value is not None: | ||
| yield CustomEvent(name=_INTERRUPT_CARD_EVENT_NAME, value=interrupt_event_value) | ||
| yield _build_run_finished_event(run_id=run_id, thread_id=thread_id, interrupts=pending_interrupts) | ||
|
Member
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. is this function no longer relevant? or can we include the checkpoint logic in it instead of adding a new one? |
||
| baseline_checkpoint_id = ( | ||
| workflow.get_last_checkpoint_id() if hasattr(workflow, "get_last_checkpoint_id") else None | ||
| ) | ||
| yield await _build_run_finished_with_checkpointed_interrupts( | ||
| run_id=run_id, | ||
| thread_id=thread_id, | ||
| interrupts=pending_interrupts, | ||
| workflow=workflow, | ||
| checkpoint_storage=checkpoint_storage, | ||
| known_checkpoint_id=checkpoint_id, | ||
| baseline_checkpoint_id=baseline_checkpoint_id, | ||
| ) | ||
| return | ||
|
|
||
| if checkpoint_id is None and not responses and not messages: | ||
| yield RunStartedEvent(run_id=run_id, thread_id=thread_id) | ||
| yield _build_run_finished_event(run_id=run_id, thread_id=thread_id, interrupts=pending_interrupts) | ||
| baseline_checkpoint_id = ( | ||
| workflow.get_last_checkpoint_id() if hasattr(workflow, "get_last_checkpoint_id") else None | ||
| ) | ||
| yield await _build_run_finished_with_checkpointed_interrupts( | ||
| run_id=run_id, | ||
| thread_id=thread_id, | ||
| interrupts=pending_interrupts, | ||
| workflow=workflow, | ||
| checkpoint_storage=checkpoint_storage, | ||
| known_checkpoint_id=checkpoint_id, | ||
| baseline_checkpoint_id=baseline_checkpoint_id, | ||
| ) | ||
| return | ||
|
|
||
| def _drain_open_message() -> list[TextMessageEndEvent]: | ||
|
|
@@ -1195,6 +1318,8 @@ def _drain_open_blocks() -> list[BaseEvent]: | |
| if checkpoint_storage is not None or checkpoint_id is not None: | ||
| checkpoint_kwargs = {"checkpoint_storage": checkpoint_storage, "checkpoint_id": checkpoint_id} | ||
|
|
||
| baseline_checkpoint_id = workflow.get_last_checkpoint_id() if hasattr(workflow, "get_last_checkpoint_id") else None | ||
|
|
||
| try: | ||
| telemetry_conversation_id = str(supplied_thread_id) if supplied_thread_id is not None else None | ||
| telemetry_context = partial(_use_telemetry_conversation_id, telemetry_conversation_id) | ||
|
|
@@ -1244,7 +1369,15 @@ def _drain_open_blocks() -> list[BaseEvent]: | |
| yield end_event | ||
| if not interrupts: | ||
| interrupts.extend(_interrupts_from_pending_requests(await _pending_request_events(workflow))) | ||
| yield _build_run_finished_event(run_id=run_id, thread_id=thread_id, interrupts=interrupts) | ||
| yield await _build_run_finished_with_checkpointed_interrupts( | ||
| run_id=run_id, | ||
| thread_id=thread_id, | ||
| interrupts=interrupts, | ||
| workflow=workflow, | ||
| checkpoint_storage=checkpoint_storage, | ||
| known_checkpoint_id=checkpoint_id, | ||
| baseline_checkpoint_id=baseline_checkpoint_id, | ||
| ) | ||
| terminal_emitted = True | ||
| elif state_value not in _TERMINAL_STATES: | ||
| yield CustomEvent(name="status", value={"state": state_value}) | ||
|
|
@@ -1391,4 +1524,12 @@ def _drain_open_blocks() -> list[BaseEvent]: | |
| if not terminal_emitted and not run_error_emitted: | ||
| if not interrupts: | ||
| interrupts.extend(_interrupts_from_pending_requests(await _pending_request_events(workflow))) | ||
| yield _build_run_finished_event(run_id=run_id, thread_id=thread_id, interrupts=interrupts) | ||
| yield await _build_run_finished_with_checkpointed_interrupts( | ||
| run_id=run_id, | ||
| thread_id=thread_id, | ||
| interrupts=interrupts, | ||
| workflow=workflow, | ||
| checkpoint_storage=checkpoint_storage, | ||
| known_checkpoint_id=checkpoint_id, | ||
| baseline_checkpoint_id=baseline_checkpoint_id, | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
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.
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.
no need for the issue here