Repository navigation
feat(cli): keep coded agents' workspace files across suspend and resume [PC-5359] #1976
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
tudormatei1
wants to merge
4
commits into
main
Choose a base branch
from
feat/PC-5359-coded-workspace-persistence
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
4 commits
Select commit
Hold shift + click to select a range
df0f6e4
feat(cli): keep coded agents' workspace files across suspend and resume
tudormatei1 46d6959
test(cli): drop workspace tests covered at another layer
tudormatei1 56e9b80
chore(uipath): require uipath-runtime 0.13.8
tudormatei1 7ae4f93
test(cli): check that a resumed run gets its files back
tudormatei1 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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| """Host-managed workspace persistence for runtimes that opt in. | ||
|
|
||
| A factory opts in through ``UiPathRuntimeFactorySettings.managed_workspace``. | ||
| The host then gives each job run a workspace folder, exposed to agent code | ||
| through ``uipath.runtime.get_workspace_path()``, and persists the folder as | ||
| job attachments across suspend and resume. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from uipath.platform import UiPath | ||
| from uipath.platform.common import UiPathConfig | ||
| from uipath.runtime import ( | ||
| HydrationPolicy, | ||
| ManagedWorkspace, | ||
| UiPathRuntimeFactoryProtocol, | ||
| UiPathRuntimeFactorySettings, | ||
| UiPathRuntimeProtocol, | ||
| WorkspaceRegistryStore, | ||
| ) | ||
|
|
||
| __all__ = ["wrap_with_managed_workspace"] | ||
|
|
||
|
|
||
| async def wrap_with_managed_workspace( | ||
| runtime: UiPathRuntimeProtocol, | ||
| *, | ||
| factory: UiPathRuntimeFactoryProtocol, | ||
| settings: UiPathRuntimeFactorySettings | None, | ||
| runtime_id: str, | ||
| job_id: str | None, | ||
| conversation_id: str | None = None, | ||
| ) -> UiPathRuntimeProtocol: | ||
| """Wrap a runtime so its workspace survives suspend and resume. | ||
|
|
||
| The runtime is returned unchanged unless the factory opts in, the run is a | ||
| job, and the factory has storage for the workspace registry. Files are | ||
| uploaded when the run suspends, and also when a conversational exchange | ||
| ends successfully, because the next exchange resumes the same job. | ||
|
|
||
| Args: | ||
| runtime: Runtime returned by the factory. | ||
| factory: Factory that created the runtime; provides registry storage. | ||
| settings: The factory's settings. | ||
| runtime_id: Runtime id the runtime was created with. | ||
| job_id: Orchestrator job key, or ``None`` outside a job. | ||
| conversation_id: Conversation id for conversational runs. | ||
|
|
||
| Returns: | ||
| The wrapped runtime, or ``runtime`` when no workspace applies. | ||
| """ | ||
| if settings is None or not settings.managed_workspace or not job_id: | ||
| return runtime | ||
|
|
||
| storage = await factory.get_storage() | ||
| if storage is None: | ||
| return runtime | ||
|
|
||
| managed = ManagedWorkspace( | ||
| services=UiPath, | ||
| runtime_id=runtime_id, | ||
| job_key=job_id, | ||
| folder_key=UiPathConfig.folder_key, | ||
| registry_store=WorkspaceRegistryStore(storage, runtime_id), | ||
| ) | ||
| return managed.wrap( | ||
| runtime, | ||
| policy=( | ||
| HydrationPolicy.SUSPEND_OR_SUCCESS | ||
| if conversation_id | ||
| else HydrationPolicy.SUSPEND_ONLY | ||
| ), | ||
| ) | ||
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
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
83 changes: 83 additions & 0 deletions
83
packages/uipath/tests/cli/eval/test_eval_runtime_managed_workspace.py
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 |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import uuid | ||
| from pathlib import Path | ||
| from typing import Any | ||
|
|
||
| from tests.cli.utils.workspace_fakes import ( | ||
| OPTED_IN, | ||
| REGISTRY, | ||
| FakeSdk, | ||
| FileRuntime, | ||
| MemoryStorage, | ||
| WorkspaceFactory, | ||
| ) | ||
| from uipath.core.events import EventBus | ||
| from uipath.core.tracing import UiPathTraceManager | ||
| from uipath.eval.helpers import EvalHelpers | ||
| from uipath.eval.runtime import UiPathEvalContext, evaluate | ||
| from uipath.eval.runtime.events import EvaluationEvents | ||
| from uipath.runtime import UiPathRuntimeFactorySettings, UiPathRuntimeStatus | ||
|
|
||
| JOB_ID = str(uuid.uuid4()) | ||
| EVAL_SET = Path(__file__).parent / "evals" / "eval-sets" / "default.json" | ||
|
|
||
|
|
||
| async def _eval_context(items: int = 1) -> UiPathEvalContext: | ||
| evaluation_set, _ = EvalHelpers.load_eval_set(str(EVAL_SET)) | ||
| first = evaluation_set.evaluations[0] | ||
| evaluation_set.evaluations = [first] + [ | ||
| first.model_copy(update={"id": str(uuid.uuid4())}) for _ in range(items - 1) | ||
| ] | ||
| context = UiPathEvalContext() | ||
| context.execution_id = str(uuid.uuid4()) | ||
| context.job_id = JOB_ID | ||
| context.evaluation_set = evaluation_set | ||
| context.runtime_schema = await FileRuntime().get_schema() | ||
| context.evaluators = await EvalHelpers.load_evaluators( | ||
| str(EVAL_SET), evaluation_set, agent_model=None | ||
| ) | ||
| return context | ||
|
|
||
|
|
||
| def _suspending_factory( | ||
| storage: MemoryStorage, | ||
| settings: UiPathRuntimeFactorySettings | None = OPTED_IN, | ||
| ) -> WorkspaceFactory: | ||
| return WorkspaceFactory( | ||
| FileRuntime(write="draft", status=UiPathRuntimeStatus.SUSPENDED), | ||
| storage=storage, | ||
| settings=settings, | ||
| ) | ||
|
|
||
|
|
||
| async def test_eval_uploads_each_suspended_item_workspace(sdk: FakeSdk) -> None: | ||
| context = await _eval_context(items=2) | ||
| storage = MemoryStorage() | ||
|
|
||
| await evaluate( | ||
| _suspending_factory(storage), UiPathTraceManager(), context, EventBus() | ||
| ) | ||
|
|
||
| item_ids = [item.id for item in context.evaluation_set.evaluations] | ||
| assert list(sdk.attachments.blobs.values()) == [b"draft"] * len(item_ids) | ||
| assert {key[0] for key in storage.values if key[1:] == REGISTRY} == set(item_ids) | ||
|
|
||
|
|
||
| async def test_eval_set_run_carries_the_factory_agent_type(sdk: FakeSdk) -> None: | ||
| created: list[Any] = [] | ||
| event_bus = EventBus() | ||
| event_bus.subscribe(EvaluationEvents.CREATE_EVAL_SET_RUN, created.append) | ||
| settings = UiPathRuntimeFactorySettings( | ||
| managed_workspace=True, agent_type="uipath_coded" | ||
| ) | ||
|
|
||
| await evaluate( | ||
| _suspending_factory(MemoryStorage(), settings), | ||
| UiPathTraceManager(), | ||
| await _eval_context(), | ||
| event_bus, | ||
| ) | ||
| await event_bus.wait_for_all() | ||
|
|
||
| assert [event.agent_type for event in created] == ["uipath_coded"] |
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.
Right that the import needs a runtime with
ManagedWorkspacewhile the lock picked 0.13.5. 0.13.7 wouldn't fix it, though: it was released from main withoutManagedWorkspace. The branch now requiresuipath-runtime>=0.13.8(56e9b80). The lock gets regenerated once 0.13.8 (UiPath/uipath-runtime-python#175) is on PyPI, as the description says.