-
Notifications
You must be signed in to change notification settings - Fork 35
feat: propagate reference hierarchy context from LangGraph runtime #901
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
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
ecbcf35
feat(traceview): propagate reference hierarchy context from LangGraph…
jepadil23 c6cdc6a
feat(traceview): make changes for copilot comments
jepadil23 65fdb0c
feat(traceview): handling reference context not added yet
jepadil23 26f05c0
fix(traceview): Fix from statements
jepadil23 c977399
fix(traceview): handle comments from error
jepadil23 8fd5dba
fix(traceview): fix module order
jepadil23 a7c71b0
fix(traceview): fix modules
jepadil23 b599380
fix(traceview): fix module order
jepadil23 7e1f7d8
fix(traceview): fix unit tests
jepadil23 b576313
fix(traceview): fix dump module thing
jepadil23 1bb9a63
fix(traceview): format
jepadil23 3db217f
feat(traceview): update versions
jepadil23 730bb4b
fix(traceview): revert package
jepadil23 5372149
Merge branch 'main' into feat/reference-hierarchy-context
jepadil23 f46cc40
feat(traceview): update uv lock
jepadil23 2999576
feat(traceview): update lock
jepadil23 46ca34b
Revert "feat(traceview): update lock"
jepadil23 51a2eb6
fix(traceview): update url
jepadil23 35ed707
fix(traceview): fix module
jepadil23 dcb6939
fix(traceview): modules
jepadil23 9e2745b
fix(traceview): fix assignments
jepadil23 9011694
fix(traceview): fix mypy
jepadil23 65f8924
fix(traceview): modules
jepadil23 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,242 @@ | ||
| """Tests for ReferenceContext wiring in UiPathLangGraphRuntime.""" | ||
|
|
||
| from typing import Any, TypedDict | ||
|
|
||
| import pytest | ||
| from langgraph.checkpoint.sqlite.aio import AsyncSqliteSaver | ||
| from langgraph.graph import END, START, StateGraph | ||
|
|
||
| from uipath_langchain.runtime.errors import LangGraphRuntimeError | ||
| from uipath_langchain.runtime.runtime import UiPathLangGraphRuntime | ||
|
|
||
| ReferenceContext: Any = None | ||
| ReferenceContextAccessor: Any = None | ||
| _reference_context_available = False | ||
|
|
||
| try: | ||
| from uipath.tracing import ( | ||
| ReferenceContext, | ||
| ReferenceContextAccessor, | ||
| ) | ||
|
|
||
| _reference_context_available = True | ||
| except ImportError: | ||
| pass | ||
|
|
||
| pytestmark = pytest.mark.skipif( | ||
| not _reference_context_available, | ||
| reason="installed uipath does not export ReferenceContext", | ||
| ) | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Minimal graph fixture | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| class _State(TypedDict): | ||
| value: str | ||
|
|
||
|
|
||
| def _build_graph() -> Any: | ||
| graph = StateGraph(_State) | ||
| graph.add_node("step", lambda s: {"value": s.get("value", "") + "_done"}) | ||
| graph.add_edge(START, "step") | ||
| graph.add_edge("step", END) | ||
| return graph | ||
|
|
||
|
|
||
| def _clear_accessor() -> None: | ||
| ReferenceContextAccessor.set(None) | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # _push_reference_context — unit tests (no graph needed) | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| class TestPushReferenceContext: | ||
| def setup_method(self) -> None: | ||
| _clear_accessor() | ||
|
|
||
| def teardown_method(self) -> None: | ||
| _clear_accessor() | ||
|
|
||
| def test_sets_langgraph_entry_when_agent_id_present( | ||
| self, monkeypatch: pytest.MonkeyPatch, tmp_path | ||
| ) -> None: | ||
| monkeypatch.setenv("UIPATH_AGENT_ID", "550e8400-e29b-41d4-a716-446655440020") | ||
| monkeypatch.delenv("UIPATH_PROCESS_VERSION", raising=False) | ||
|
|
||
| graph = _build_graph().compile() | ||
| runtime = UiPathLangGraphRuntime(graph=graph, runtime_id="t") | ||
|
|
||
| token = runtime._push_reference_context() | ||
| try: | ||
| ctx = ReferenceContextAccessor.get() | ||
| assert ctx is not None | ||
| assert len(ctx) == 1 | ||
| assert ctx.entries[0].service_type == "langgraph" | ||
| assert ctx.entries[0].reference_id == "550e8400-e29b-41d4-a716-446655440020" | ||
| assert ctx.entries[0].version is None | ||
| finally: | ||
| ReferenceContextAccessor.reset(token) | ||
|
|
||
| def test_includes_version_when_env_set( | ||
| self, monkeypatch: pytest.MonkeyPatch | ||
| ) -> None: | ||
| monkeypatch.setenv("UIPATH_AGENT_ID", "550e8400-e29b-41d4-a716-446655440020") | ||
| monkeypatch.setenv("UIPATH_PROCESS_VERSION", "3.1.0") | ||
|
|
||
| graph = _build_graph().compile() | ||
| runtime = UiPathLangGraphRuntime(graph=graph, runtime_id="t") | ||
|
|
||
| token = runtime._push_reference_context() | ||
| try: | ||
| ctx = ReferenceContextAccessor.get() | ||
| assert ctx is not None | ||
| assert ctx.entries[0].version == "3.1.0" | ||
| finally: | ||
| ReferenceContextAccessor.reset(token) | ||
|
|
||
| def test_no_entry_when_agent_id_absent( | ||
| self, monkeypatch: pytest.MonkeyPatch | ||
| ) -> None: | ||
| monkeypatch.delenv("UIPATH_AGENT_ID", raising=False) | ||
| monkeypatch.delenv("UIPATH_PROCESS_VERSION", raising=False) | ||
|
|
||
| graph = _build_graph().compile() | ||
| runtime = UiPathLangGraphRuntime(graph=graph, runtime_id="t") | ||
|
|
||
| token = runtime._push_reference_context() | ||
| try: | ||
| ctx = ReferenceContextAccessor.get() | ||
| assert ctx is not None | ||
| assert len(ctx) == 0 | ||
| finally: | ||
| ReferenceContextAccessor.reset(token) | ||
|
|
||
| def test_stacks_on_top_of_parent_context( | ||
| self, monkeypatch: pytest.MonkeyPatch | ||
| ) -> None: | ||
| monkeypatch.setenv("UIPATH_AGENT_ID", "550e8400-e29b-41d4-a716-446655440020") | ||
| monkeypatch.delenv("UIPATH_PROCESS_VERSION", raising=False) | ||
|
|
||
| parent = ReferenceContext.Empty.add( | ||
| "agent", "550e8400-e29b-41d4-a716-446655440001", "1.0" | ||
| ) | ||
| parent_token = ReferenceContextAccessor.set(parent) | ||
|
|
||
| graph = _build_graph().compile() | ||
| runtime = UiPathLangGraphRuntime(graph=graph, runtime_id="t") | ||
|
|
||
| token = runtime._push_reference_context() | ||
| try: | ||
| ctx = ReferenceContextAccessor.get() | ||
| assert ctx is not None | ||
| assert len(ctx) == 2 | ||
| assert ctx.entries[0].service_type == "agent" | ||
| assert ctx.entries[1].service_type == "langgraph" | ||
| finally: | ||
| ReferenceContextAccessor.reset(token) | ||
| ReferenceContextAccessor.reset(parent_token) | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # execute() — context cleared after run | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| async def test_context_cleared_after_execute( | ||
| monkeypatch: pytest.MonkeyPatch, tmp_path | ||
| ) -> None: | ||
| _clear_accessor() | ||
| monkeypatch.setenv("UIPATH_AGENT_ID", "550e8400-e29b-41d4-a716-446655440020") | ||
| monkeypatch.delenv("UIPATH_PROCESS_VERSION", raising=False) | ||
|
|
||
| async with AsyncSqliteSaver.from_conn_string(str(tmp_path / "mem.db")) as memory: | ||
| await memory.setup() | ||
| graph = _build_graph().compile(checkpointer=memory) | ||
| runtime = UiPathLangGraphRuntime(graph=graph, runtime_id="exec-run") | ||
| await runtime.execute(input={"value": "hello"}) | ||
|
|
||
| assert ReferenceContextAccessor.get() is None | ||
|
|
||
|
|
||
| async def test_context_cleared_after_execute_on_error( | ||
| monkeypatch: pytest.MonkeyPatch, tmp_path | ||
| ) -> None: | ||
| _clear_accessor() | ||
| monkeypatch.setenv("UIPATH_AGENT_ID", "550e8400-e29b-41d4-a716-446655440020") | ||
|
|
||
| class _S(TypedDict): | ||
| v: str | ||
|
|
||
| def _boom(s: _S) -> _S: | ||
| raise ValueError("explode") | ||
|
|
||
| g = StateGraph(_S) | ||
| g.add_node("boom", _boom) # type: ignore[arg-type] | ||
| g.add_edge(START, "boom") | ||
| g.add_edge("boom", END) | ||
|
|
||
| async with AsyncSqliteSaver.from_conn_string(str(tmp_path / "mem.db")) as memory: | ||
| await memory.setup() | ||
| compiled = g.compile(checkpointer=memory) | ||
| runtime = UiPathLangGraphRuntime(graph=compiled, runtime_id="err-run") | ||
| with pytest.raises(LangGraphRuntimeError): | ||
| await runtime.execute(input={"v": "x"}) | ||
|
|
||
| assert ReferenceContextAccessor.get() is None | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # stream() — context cleared after run | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| async def test_context_cleared_after_stream( | ||
| monkeypatch: pytest.MonkeyPatch, tmp_path | ||
| ) -> None: | ||
| _clear_accessor() | ||
| monkeypatch.setenv("UIPATH_AGENT_ID", "550e8400-e29b-41d4-a716-446655440020") | ||
| monkeypatch.delenv("UIPATH_PROCESS_VERSION", raising=False) | ||
|
|
||
| async with AsyncSqliteSaver.from_conn_string(str(tmp_path / "mem.db")) as memory: | ||
| await memory.setup() | ||
| graph = _build_graph().compile(checkpointer=memory) | ||
| runtime = UiPathLangGraphRuntime(graph=graph, runtime_id="stream-run") | ||
| async for _ in runtime.stream(input={"value": "hi"}): | ||
| pass | ||
|
|
||
| assert ReferenceContextAccessor.get() is None | ||
|
|
||
|
|
||
| async def test_context_cleared_after_stream_on_error( | ||
| monkeypatch: pytest.MonkeyPatch, tmp_path | ||
| ) -> None: | ||
| _clear_accessor() | ||
| monkeypatch.setenv("UIPATH_AGENT_ID", "550e8400-e29b-41d4-a716-446655440020") | ||
|
|
||
| class _S(TypedDict): | ||
| v: str | ||
|
|
||
| def _boom(s: _S) -> _S: | ||
| raise ValueError("explode") | ||
|
|
||
| g = StateGraph(_S) | ||
| g.add_node("boom", _boom) # type: ignore[arg-type] | ||
| g.add_edge(START, "boom") | ||
| g.add_edge("boom", END) | ||
|
|
||
| async with AsyncSqliteSaver.from_conn_string( | ||
| str(tmp_path / "stream-err.db") | ||
| ) as memory: | ||
| await memory.setup() | ||
| compiled = g.compile(checkpointer=memory) | ||
| runtime = UiPathLangGraphRuntime(graph=compiled, runtime_id="stream-err-run") | ||
| with pytest.raises(LangGraphRuntimeError): | ||
| async for _ in runtime.stream(input={"v": "x"}): | ||
| pass | ||
|
|
||
| assert ReferenceContextAccessor.get() is None | ||
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.
Uh oh!
There was an error while loading. Please reload this page.