-
Notifications
You must be signed in to change notification settings - Fork 579
feat(adk): propagate parent + root context_id headers on outbound A2A calls #1927
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
Prefix
wants to merge
3
commits into
kagent-dev:main
Choose a base branch
from
Prefix:feat/a2a-conversation-lineage-headers
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.
+425
−6
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| package tools | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/a2aproject/a2a-go/a2aclient" | ||
| "github.com/a2aproject/a2a-go/a2asrv" | ||
| ) | ||
|
|
||
| // newReq returns an empty outbound client Request with an initialized CallMeta. | ||
| func newReq() *a2aclient.Request { | ||
| return &a2aclient.Request{Meta: a2aclient.CallMeta{}} | ||
| } | ||
|
|
||
| // withCallContext returns a context that carries an a2asrv.CallContext whose | ||
| // RequestMeta exposes the given inbound headers, so the interceptor's | ||
| // CallContextFrom + RequestMeta path can be exercised. | ||
| func withCallContext(parent context.Context, inbound map[string][]string) context.Context { | ||
| ctx, _ := a2asrv.WithCallContext(parent, a2asrv.NewRequestMeta(inbound)) | ||
| return ctx | ||
| } | ||
|
|
||
| // TestLineageHeaderPropagation covers the parent + root context_id header | ||
| // derivation. Mirrors the Python TestLineageHeaderPropagation cases in | ||
| // python/packages/kagent-adk/tests/unittests/test_remote_a2a_tool.py. | ||
| func TestLineageHeaderPropagation(t *testing.T) { | ||
| const ownSession = "own-session-123" | ||
| const upstreamRoot = "root-from-upstream" | ||
| const upstreamParent = "parent-from-upstream" | ||
|
|
||
| t.Run("chain root stamps own id as parent and root", func(t *testing.T) { | ||
| ctx := context.WithValue(context.Background(), parentContextIDContextKey{}, ownSession) | ||
| req := newReq() | ||
|
|
||
| if _, err := (&lineageHeadersInterceptor{}).Before(ctx, req); err != nil { | ||
| t.Fatalf("Before returned error: %v", err) | ||
| } | ||
|
|
||
| assertSingleHeader(t, req, ParentContextIDHeader, ownSession) | ||
| assertSingleHeader(t, req, RootContextIDHeader, ownSession) | ||
| }) | ||
|
|
||
| t.Run("mid-chain forwards root unchanged and overrides parent with own id", func(t *testing.T) { | ||
| ctx := context.WithValue(context.Background(), parentContextIDContextKey{}, ownSession) | ||
| ctx = withCallContext(ctx, map[string][]string{ | ||
| RootContextIDHeader: {upstreamRoot}, | ||
| ParentContextIDHeader: {upstreamParent}, | ||
| }) | ||
| req := newReq() | ||
|
|
||
| if _, err := (&lineageHeadersInterceptor{}).Before(ctx, req); err != nil { | ||
| t.Fatalf("Before returned error: %v", err) | ||
| } | ||
|
|
||
| assertSingleHeader(t, req, ParentContextIDHeader, ownSession) | ||
| assertSingleHeader(t, req, RootContextIDHeader, upstreamRoot) | ||
| }) | ||
|
|
||
| t.Run("legacy inbound with only parent header promotes it to root", func(t *testing.T) { | ||
| ctx := context.WithValue(context.Background(), parentContextIDContextKey{}, ownSession) | ||
| ctx = withCallContext(ctx, map[string][]string{ | ||
| ParentContextIDHeader: {upstreamParent}, | ||
| }) | ||
| req := newReq() | ||
|
|
||
| if _, err := (&lineageHeadersInterceptor{}).Before(ctx, req); err != nil { | ||
| t.Fatalf("Before returned error: %v", err) | ||
| } | ||
|
|
||
| assertSingleHeader(t, req, ParentContextIDHeader, ownSession) | ||
| assertSingleHeader(t, req, RootContextIDHeader, upstreamParent) | ||
| }) | ||
|
|
||
| t.Run("no session id is a no-op", func(t *testing.T) { | ||
| // No parentContextIDContextKey on ctx - matches the stub tool_context | ||
| // case in Python (empty dict, no headers stamped). | ||
| ctx := context.Background() | ||
| req := newReq() | ||
|
|
||
| if _, err := (&lineageHeadersInterceptor{}).Before(ctx, req); err != nil { | ||
| t.Fatalf("Before returned error: %v", err) | ||
| } | ||
|
|
||
| if got := req.Meta.Get(ParentContextIDHeader); len(got) != 0 { | ||
| t.Errorf("expected no parent header, got %v", got) | ||
| } | ||
| if got := req.Meta.Get(RootContextIDHeader); len(got) != 0 { | ||
| t.Errorf("expected no root header, got %v", got) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("pre-existing header on req.Meta wins over lineage", func(t *testing.T) { | ||
| // Analogous to Python's header_provider override: a caller-supplied | ||
| // header that is already present on the outbound request must not be | ||
| // overwritten by the lineage interceptor. | ||
| ctx := context.WithValue(context.Background(), parentContextIDContextKey{}, ownSession) | ||
| ctx = withCallContext(ctx, map[string][]string{ | ||
| RootContextIDHeader: {upstreamRoot}, | ||
| }) | ||
| req := newReq() | ||
| req.Meta.Append(ParentContextIDHeader, "caller-override-parent") | ||
| req.Meta.Append(RootContextIDHeader, "caller-override-root") | ||
|
|
||
| if _, err := (&lineageHeadersInterceptor{}).Before(ctx, req); err != nil { | ||
| t.Fatalf("Before returned error: %v", err) | ||
| } | ||
|
|
||
| assertSingleHeader(t, req, ParentContextIDHeader, "caller-override-parent") | ||
| assertSingleHeader(t, req, RootContextIDHeader, "caller-override-root") | ||
| }) | ||
| } | ||
|
|
||
| func assertSingleHeader(t *testing.T, req *a2aclient.Request, key, want string) { | ||
| t.Helper() | ||
| got := req.Meta.Get(key) | ||
| if len(got) != 1 { | ||
| t.Fatalf("%s: expected exactly 1 value, got %v", key, got) | ||
| } | ||
| if got[0] != want { | ||
| t.Errorf("%s: got %q, want %q", key, got[0], want) | ||
| } | ||
| } |
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
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.