Skip to content

Commit 5720d31

Browse files
committed
fixup(adk): enforce empty-dict contract when parent context_id missing + annotate test helper
Addresses Copilot review feedback on kagent-dev#1927. - _build_lineage_headers now hard-returns {} when the local session id is unresolvable. Without this guard the function would emit a root-only header forwarded from inbound state, contradicting the docstring contract "no session id → no lineage headers" and matching the existing unit-test assertion. - _make_tool's header_provider parameter gains Callable type annotation matching the production signature. Signed-off-by: Lukas <1775218+Prefix@users.noreply.github.com>
1 parent f4ffc6a commit 5720d31

2 files changed

Lines changed: 9 additions & 8 deletions

File tree

python/packages/kagent-adk/src/kagent/adk/_remote_a2a_tool.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -285,18 +285,19 @@ def _build_lineage_headers(self, tool_context: ToolContext) -> dict[str, str]:
285285
if isinstance(hdrs, dict):
286286
inbound_headers = hdrs
287287

288+
if not parent_context_id:
289+
return {}
290+
288291
root_context_id = (
289292
inbound_headers.get(ROOT_CONTEXT_ID_HEADER)
290293
or inbound_headers.get(PARENT_CONTEXT_ID_HEADER)
291294
or parent_context_id
292295
)
293296

294-
headers: dict[str, str] = {}
295-
if parent_context_id:
296-
headers[PARENT_CONTEXT_ID_HEADER] = str(parent_context_id)
297-
if root_context_id:
298-
headers[ROOT_CONTEXT_ID_HEADER] = str(root_context_id)
299-
return headers
297+
return {
298+
PARENT_CONTEXT_ID_HEADER: str(parent_context_id),
299+
ROOT_CONTEXT_ID_HEADER: str(root_context_id),
300+
}
300301

301302
async def run_async(self, *, args: dict[str, Any], tool_context: ToolContext) -> Any:
302303
"""Execute the remote agent tool.

python/packages/kagent-adk/tests/unittests/test_remote_a2a_tool.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Tests for KAgentRemoteA2ATool."""
22

3-
from typing import Any, AsyncIterator
3+
from typing import Any, AsyncIterator, Callable
44
from unittest.mock import AsyncMock, MagicMock, patch
55

66
import httpx
@@ -121,7 +121,7 @@ async def _async_yield(*items) -> AsyncIterator:
121121
def _make_tool(
122122
*,
123123
httpx_client: httpx.AsyncClient | None = None,
124-
header_provider=None,
124+
header_provider: Callable[[Any], dict[str, str]] | None = None,
125125
) -> KAgentRemoteA2ATool:
126126
return KAgentRemoteA2ATool(
127127
name="k8s_agent",

0 commit comments

Comments
 (0)