Skip to content

feat: propagate reference hierarchy context from LangGraph runtime#901

Open
jepadil23 wants to merge 1 commit into
mainfrom
feat/reference-hierarchy-context
Open

feat: propagate reference hierarchy context from LangGraph runtime#901
jepadil23 wants to merge 1 commit into
mainfrom
feat/reference-hierarchy-context

Conversation

@jepadil23

Copy link
Copy Markdown

Summary

  • UiPathLangGraphRuntime._push_reference_context() pushes a "langgraph" entry into ReferenceContextAccessor during execute() and stream(), resets in finally
  • All spans emitted by LangGraph nodes within the run automatically carry the full reference hierarchy (maestro → agent → langgraph) in their Context.referenceHierarchy wire payload

Dependencies

Requires uipath-python PR #1708 (ReferenceContext / ReferenceContextAccessor types).

Test plan

  • tests/runtime/test_reference_context_wiring.py — verifies referenceHierarchy is present in LangGraph runtime spans with correct serviceType="langgraph" entry
  • Verify context resets correctly after execute/stream completes

🤖 Generated with Claude Code

UiPathLangGraphRuntime._push_reference_context() reads any parent
ReferenceContext set by upstream (e.g. agents-python start_agent_run),
appends a "langgraph" entry from UIPATH_AGENT_ID / UIPATH_PROCESS_VERSION,
and activates it via ReferenceContextAccessor. Both execute() and
stream() call this at entry and reset in finally, so all child spans
emitted during the run carry the full hierarchy in their
Context.referenceHierarchy wire payload.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings June 10, 2026 19:14
@sonarqubecloud

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
0.0% Coverage on New Code (required ≥ 90%)

See analysis details on SonarQube Cloud

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds propagation of an ambient ReferenceContext for UiPathLangGraphRuntime runs so that downstream tracing can attribute spans to a langgraph service entry layered on top of any parent reference hierarchy.

Changes:

  • Add UiPathLangGraphRuntime._push_reference_context() and wrap execute()/stream() with push/reset in try/finally.
  • Introduce a new test module covering context stacking behavior and ensuring the context is reset after execute()/stream() (including error paths).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 9 comments.

File Description
src/uipath_langchain/runtime/runtime.py Pushes a langgraph entry into ReferenceContextAccessor during execute()/stream() and resets it in finally.
tests/runtime/test_reference_context_wiring.py Adds tests for _push_reference_context() and for context reset after runtime execution/streaming.

self.chat.client_side_tools = self._get_client_side_tools()
self._middleware_node_names: set[str] = self._detect_middleware_nodes()

def _push_reference_context(self) -> contextvars.Token:
Comment on lines +11 to +14
from uipath.platform.common._reference_context import (
ReferenceContext,
ReferenceContextAccessor,
)
Comment on lines +3 to +5
import os
import tempfile
from typing import Any, TypedDict
Comment on lines +34 to +36
def _clear_accessor() -> None:
token = ReferenceContextAccessor.set(None)
ReferenceContextAccessor.reset(token)
monkeypatch.setenv("UIPATH_AGENT_ID", "550e8400-e29b-41d4-a716-446655440020")
monkeypatch.delenv("UIPATH_PROCESS_VERSION", raising=False)

from langgraph.graph import StateGraph
Comment on lines +142 to +144
with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
db = f.name

Comment on lines +171 to +173
with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
db = f.name

Comment on lines +195 to +197
with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
db = f.name

@@ -0,0 +1,205 @@
"""Tests for ReferenceContext wiring in UiPathLangGraphRuntime."""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants