Derives the node path and run ID.
(
node_name: str | None,
run_id: str,
node_path: str | None,
parent_path: str | None,
*,
node: BaseNode | None = None,
)
| 66 | |
| 67 | |
| 68 | def _derive_node_path( |
| 69 | node_name: str | None, |
| 70 | run_id: str, |
| 71 | node_path: str | None, |
| 72 | parent_path: str | None, |
| 73 | *, |
| 74 | node: BaseNode | None = None, |
| 75 | ) -> tuple[str, str]: |
| 76 | """Derives the node path and run ID.""" |
| 77 | if node_path: |
| 78 | return node_path, run_id |
| 79 | |
| 80 | # Fallback: Reconstruct parent_path from static parent_agent Tree |
| 81 | # if parent_path is missing during multi-turn session resumption. |
| 82 | from ..agents.base_agent import BaseAgent |
| 83 | from ..events._node_path_builder import _NodePathBuilder |
| 84 | |
| 85 | derived_run_id = run_id or '1' |
| 86 | |
| 87 | if not parent_path and isinstance(node, BaseAgent) and node.parent_agent: |
| 88 | path_builder = _NodePathBuilder([]) |
| 89 | curr = node.parent_agent |
| 90 | parent_agents = [] |
| 91 | depth = 0 |
| 92 | while curr is not None and depth < _MAX_PARENT_DEPTH: |
| 93 | parent_agents.insert(0, curr) |
| 94 | curr = curr.parent_agent |
| 95 | depth += 1 |
| 96 | for agent in parent_agents: |
| 97 | path_builder = path_builder.append(agent.name, '1') |
| 98 | parent_path = str(path_builder) |
| 99 | |
| 100 | # Root contexts have no node name and no parent path. Return an empty path |
| 101 | # to ensure they are correctly identified as the root of the execution |
| 102 | # hierarchy. |
| 103 | if not node_name and not parent_path: |
| 104 | return '', derived_run_id |
| 105 | |
| 106 | base_path_builder = ( |
| 107 | _NodePathBuilder.from_string(parent_path) |
| 108 | if parent_path |
| 109 | else _NodePathBuilder([]) |
| 110 | ) |
| 111 | |
| 112 | derived_node_path = str( |
| 113 | base_path_builder.append(node_name or '', derived_run_id) |
| 114 | ) |
| 115 | return derived_node_path, derived_run_id |
| 116 | |
| 117 | |
| 118 | class Context(ReadonlyContext): |
no test coverage detected