-
Notifications
You must be signed in to change notification settings - Fork 606
implement agent reliability features from ADK #2001
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
peterj
wants to merge
2
commits into
main
Choose a base branch
from
peterj/addreliabilityfeatures
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.
+1,789
−34
Open
Changes from all commits
Commits
Show all changes
2 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
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
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,128 @@ | ||
| package runner | ||
|
|
||
| import ( | ||
| "context" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/go-logr/logr" | ||
| "github.com/kagent-dev/kagent/go/api/adk" | ||
| "google.golang.org/adk/agent" | ||
| "google.golang.org/adk/session" | ||
| "google.golang.org/genai" | ||
| ) | ||
|
|
||
| func TestBuildReliabilityPlugins(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| config *adk.ReliabilityConfig | ||
| wantNames []string | ||
| }{ | ||
| { | ||
| name: "nil config", | ||
| config: nil, | ||
| wantNames: nil, | ||
| }, | ||
| { | ||
| name: "empty config", | ||
| config: &adk.ReliabilityConfig{}, | ||
| wantNames: nil, | ||
| }, | ||
| { | ||
| name: "debug logging only", | ||
| config: &adk.ReliabilityConfig{DebugLogging: new(true)}, | ||
| wantNames: []string{"kagent_debug_logging"}, | ||
| }, | ||
| { | ||
| name: "debug logging false", | ||
| config: &adk.ReliabilityConfig{DebugLogging: new(false)}, | ||
| wantNames: nil, | ||
| }, | ||
| { | ||
| name: "tool retries only", | ||
| config: &adk.ReliabilityConfig{ToolRetries: new(3)}, | ||
| wantNames: []string{"RetryAndReflectPlugin"}, | ||
| }, | ||
| { | ||
| name: "max llm calls only", | ||
| config: &adk.ReliabilityConfig{MaxLLMCalls: new(10)}, | ||
| wantNames: []string{"kagent_max_llm_calls"}, | ||
| }, | ||
| { | ||
| name: "all enabled", | ||
| config: &adk.ReliabilityConfig{ | ||
| ToolRetries: new(2), | ||
| MaxLLMCalls: new(50), | ||
| DebugLogging: new(true), | ||
| }, | ||
| wantNames: []string{"kagent_debug_logging", "RetryAndReflectPlugin", "kagent_max_llm_calls"}, | ||
| }, | ||
|
peterj marked this conversation as resolved.
|
||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| plugins, err := buildReliabilityPlugins(tt.config, logr.Discard()) | ||
| if err != nil { | ||
| t.Fatalf("buildReliabilityPlugins() error = %v", err) | ||
| } | ||
| if len(plugins) != len(tt.wantNames) { | ||
| t.Fatalf("got %d plugins, want %d", len(plugins), len(tt.wantNames)) | ||
| } | ||
| for i, want := range tt.wantNames { | ||
| if got := plugins[i].Name(); got != want { | ||
| t.Errorf("plugin[%d].Name() = %q, want %q", i, got, want) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // fakeCallbackContext is a minimal agent.CallbackContext for testing. | ||
| type fakeCallbackContext struct { | ||
| context.Context | ||
| invocationID string | ||
| } | ||
|
|
||
| func (f *fakeCallbackContext) UserContent() *genai.Content { return nil } | ||
| func (f *fakeCallbackContext) InvocationID() string { return f.invocationID } | ||
| func (f *fakeCallbackContext) AgentName() string { return "test-agent" } | ||
| func (f *fakeCallbackContext) ReadonlyState() session.ReadonlyState { return nil } | ||
| func (f *fakeCallbackContext) UserID() string { return "user" } | ||
| func (f *fakeCallbackContext) AppName() string { return "app" } | ||
| func (f *fakeCallbackContext) SessionID() string { return "session" } | ||
| func (f *fakeCallbackContext) Branch() string { return "" } | ||
| func (f *fakeCallbackContext) Artifacts() agent.Artifacts { return nil } | ||
| func (f *fakeCallbackContext) State() session.State { return nil } | ||
|
|
||
| func TestMaxLLMCallsPlugin(t *testing.T) { | ||
| p, err := newMaxLLMCallsPlugin(2) | ||
| if err != nil { | ||
| t.Fatalf("newMaxLLMCallsPlugin() error = %v", err) | ||
| } | ||
| cb := p.BeforeModelCallback() | ||
| if cb == nil { | ||
| t.Fatal("BeforeModelCallback is nil") | ||
| } | ||
|
|
||
| ctxA := &fakeCallbackContext{Context: context.Background(), invocationID: "inv-a"} | ||
| ctxB := &fakeCallbackContext{Context: context.Background(), invocationID: "inv-b"} | ||
|
|
||
| // First two calls within the limit succeed. | ||
| for i := range 2 { | ||
| if _, err := cb(ctxA, nil); err != nil { | ||
| t.Fatalf("call %d: unexpected error: %v", i+1, err) | ||
| } | ||
| } | ||
|
|
||
| // Third call exceeds the limit. | ||
| if _, err := cb(ctxA, nil); err == nil { | ||
| t.Fatal("expected error after exceeding limit, got nil") | ||
| } else if !strings.Contains(err.Error(), "limit of 2 model calls") { | ||
| t.Errorf("unexpected error message: %v", err) | ||
| } | ||
|
|
||
| // A different invocation has its own counter. | ||
| if _, err := cb(ctxB, nil); err != nil { | ||
| t.Fatalf("different invocation should not be limited: %v", err) | ||
| } | ||
| } | ||
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,42 @@ | ||
| package runner | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "sync" | ||
|
|
||
| "google.golang.org/adk/agent" | ||
| "google.golang.org/adk/model" | ||
| adkplugin "google.golang.org/adk/plugin" | ||
| ) | ||
|
|
||
| // newMaxLLMCallsPlugin returns a plugin that enforces a limit on the number of | ||
| // LLM calls within a single invocation. The Go ADK has no native equivalent of | ||
| // the Python RunConfig.max_llm_calls, so this is implemented as a | ||
| // BeforeModelCallback that counts model calls per invocation ID and aborts the | ||
| // run when the limit is exceeded. | ||
| func newMaxLLMCallsPlugin(limit int) (*adkplugin.Plugin, error) { | ||
| var mu sync.Mutex | ||
| counts := make(map[string]int) | ||
|
|
||
| return adkplugin.New(adkplugin.Config{ | ||
| Name: "kagent_max_llm_calls", | ||
| BeforeModelCallback: func(ctx agent.CallbackContext, llmRequest *model.LLMRequest) (*model.LLMResponse, error) { | ||
| mu.Lock() | ||
| defer mu.Unlock() | ||
| id := ctx.InvocationID() | ||
| counts[id]++ | ||
| if counts[id] > limit { | ||
| return nil, fmt.Errorf( | ||
| "agent stopped: exceeded the configured limit of %d model calls in a single run (reliability.maxLLMCalls)", | ||
| limit, | ||
| ) | ||
| } | ||
| return nil, nil | ||
| }, | ||
| AfterRunCallback: func(ictx agent.InvocationContext) { | ||
| mu.Lock() | ||
| defer mu.Unlock() | ||
| delete(counts, ictx.InvocationID()) | ||
| }, | ||
| }) | ||
| } |
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.