-
Notifications
You must be signed in to change notification settings - Fork 732
fix(substrate): make sandbox agents resume reliably #2503
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
Sam123ben
wants to merge
3
commits into
kagent-dev:main
Choose a base branch
from
Sam123ben:agent/substrate-compatibility
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.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package a2a_test | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| a2atype "github.com/a2aproject/a2a-go/v2/a2a" | ||
| a2aclient "github.com/a2aproject/a2a-go/v2/a2aclient" | ||
| ) | ||
|
|
||
| func TestJSONRPCClientDecodesSingleObjectErrorData(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| var request struct { | ||
| ID json.RawMessage `json:"id"` | ||
| } | ||
| if err := json.NewDecoder(r.Body).Decode(&request); err != nil { | ||
| t.Errorf("decode request: %v", err) | ||
| return | ||
| } | ||
|
|
||
| w.Header().Set("Content-Type", "application/json") | ||
| _, _ = w.Write([]byte(`{"jsonrpc":"2.0","id":` + string(request.ID) + `,"error":{"code":-32603,"message":"agent execution failed","data":{"@type":"type.googleapis.com/google.rpc.ErrorInfo","domain":"a2a-protocol.org","reason":"INTERNAL_ERROR"}}}`)) | ||
| })) | ||
| t.Cleanup(server.Close) | ||
|
|
||
| transport := a2aclient.NewJSONRPCTransport(server.URL, server.Client()) | ||
| _, err := transport.SendMessage(t.Context(), a2aclient.ServiceParams{}, &a2atype.SendMessageRequest{ | ||
| Message: a2atype.NewMessage(a2atype.MessageRoleUser, a2atype.NewTextPart("test")), | ||
| }) | ||
| if err == nil { | ||
| t.Fatal("SendMessage() succeeded, want application error") | ||
| } | ||
| if !strings.Contains(err.Error(), "agent execution failed") { | ||
| t.Fatalf("SendMessage() error = %q, want application error", err) | ||
| } | ||
| if strings.Contains(err.Error(), "failed to decode response") { | ||
| t.Fatalf("SendMessage() error obscured application error: %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 |
|---|---|---|
|
|
@@ -16,7 +16,6 @@ import ( | |
| "github.com/kagent-dev/kagent/go/api/v1alpha3" | ||
| "github.com/kagent-dev/kagent/go/core/internal/utils" | ||
| "github.com/kagent-dev/kagent/go/core/pkg/sandboxbackend/substrate" | ||
| ctrllog "sigs.k8s.io/controller-runtime/pkg/log" | ||
| ) | ||
|
|
||
| // substrateSandboxSessionRoundTripper routes each A2A request to the session actor identified by contextId. | ||
|
|
@@ -71,15 +70,18 @@ func (t *substrateSandboxSessionRoundTripper) RoundTrip(req *http.Request) (*htt | |
| if sessionID == "" { | ||
| return nil, fmt.Errorf("message contextId (session id) is required for substrate sandbox agents") | ||
| } | ||
| userID := strings.TrimSpace(req.Header.Get("X-User-Id")) | ||
| if userID == "" { | ||
| return nil, fmt.Errorf("request carries no user identity") | ||
| } | ||
|
|
||
| // non blocking attempt to ensure that sandbox agent session metadata is persisted to postgres | ||
| // to support session list and delete cleanup. | ||
| if err := t.ensureSessionRow(req.Context(), sessionID, req.Header.Get("X-User-Id")); err != nil { | ||
| ctrllog.FromContext(req.Context()).WithName("substrate-sandbox-transport").Error(err, | ||
| "failed to ensure session row; continuing without it", "sessionID", sessionID) | ||
| // Persist owner-scoped session metadata before creating or resuming an actor. | ||
| // Failing closed keeps untracked sessions out of the shared sandbox runtime. | ||
| if err := t.ensureSessionRow(req.Context(), sessionID, userID); err != nil { | ||
| return nil, fmt.Errorf("ensure controller session row: %w", err) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this was best effort before, now a db write failure blocks the whole chat request. a short postgres hiccup now breaks every substrate agent, not just session listing. intended tradeoff? |
||
| } | ||
|
|
||
| res, err := t.actorBackend.EnsureSessionActor(req.Context(), t.sandboxAgent, sessionID) | ||
| res, err := t.actorBackend.EnsureSessionActor(req.Context(), t.sandboxAgent, userID, sessionID) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
@@ -93,7 +95,7 @@ func (t *substrateSandboxSessionRoundTripper) RoundTrip(req *http.Request) (*htt | |
|
|
||
| resp, err := actorRT.RoundTrip(req) | ||
| if err != nil { | ||
| t.scheduleSuspendSession(sessionID) | ||
| t.scheduleSuspendSession(userID, sessionID) | ||
| return nil, err | ||
| } | ||
|
|
||
|
|
@@ -102,7 +104,7 @@ func (t *substrateSandboxSessionRoundTripper) RoundTrip(req *http.Request) (*htt | |
| resp.Body = &suspendSessionActorOnClose{ | ||
| ReadCloser: resp.Body, | ||
| suspend: func() { | ||
| t.scheduleSuspendSession(sessionID) | ||
| t.scheduleSuspendSession(userID, sessionID) | ||
| }, | ||
| } | ||
| return resp, nil | ||
|
|
@@ -132,14 +134,14 @@ func (t *substrateSandboxSessionRoundTripper) ensureSessionRow(ctx context.Conte | |
| return nil | ||
| } | ||
|
|
||
| func (t *substrateSandboxSessionRoundTripper) scheduleSuspendSession(sessionID string) { | ||
| func (t *substrateSandboxSessionRoundTripper) scheduleSuspendSession(userID, sessionID string) { | ||
| if t == nil || t.actorBackend == nil || t.sandboxAgent == nil { | ||
| return | ||
| } | ||
| go func() { | ||
| ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) | ||
| defer cancel() | ||
| _ = t.actorBackend.SuspendSessionActor(ctx, t.sandboxAgent, sessionID) | ||
| _ = t.actorBackend.SuspendSessionActor(ctx, t.sandboxAgent, userID, sessionID) | ||
| }() | ||
| } | ||
|
|
||
|
|
||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
now hard requires X-User-Id on every request through this transport. are all callers of this roundtripper guaranteed to go through auth middleware first, including any internal or background caller?