From e6284f4eacc89a4b2f1ef2469e9f86241e2841cd Mon Sep 17 00:00:00 2001 From: wizardchen Date: Thu, 24 Sep 2026 17:13:48 +0800 Subject: [PATCH] fix(browser): let shared-space agents drive the caller's local browser A shared agent runs in its owner's workspace, so the execution tenant in the turn context is the owner's. The local_browser tool built its device scope from that tenant, while the browser is paired under the asking member's own workspace, so every call from a shared agent found no device and told the user to pair again. Resolve the browser scope from the authenticated caller instead, both when registering the tool and in its owner check. Agents in the caller's own workspace are unaffected: without a tenant switch the caller and the execution tenant are the same. --- internal/agent/tools/browserskill.go | 11 ++++-- internal/agent/tools/browserskill_test.go | 38 +++++++++++++++++++ internal/application/service/agent_service.go | 4 +- 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/internal/agent/tools/browserskill.go b/internal/agent/tools/browserskill.go index 8807ab19..f3d1a7ac 100644 --- a/internal/agent/tools/browserskill.go +++ b/internal/agent/tools/browserskill.go @@ -35,6 +35,13 @@ type BrowserSkillTool struct { blocked atomic.Pointer[string] } +// BrowserSkillScope names the caller's browser. A shared agent runs in its +// owner's workspace, but the browser stays paired to the member who asked. +func BrowserSkillScope(ctx context.Context) browserskill.Scope { + caller := types.CallerFromContext(ctx) + return browserskill.Scope{Tenant: caller.TenantID, User: caller.UserID} +} + // NewBrowserSkillTool creates a session-bound adapter to upstream RPC. func NewBrowserSkillTool( manager *browserskill.Manager, @@ -56,9 +63,7 @@ func NewBrowserSkillTool( // Execute validates tool arguments and dispatches through the authorized task. func (t *BrowserSkillTool) Execute(ctx context.Context, args json.RawMessage) (*types.ToolResult, error) { - tenant, _ := types.TenantIDFromContext(ctx) - user, _ := types.UserIDFromContext(ctx) - if tenant != t.scope.Tenant || user != t.scope.User { + if BrowserSkillScope(ctx) != t.scope { return nil, errors.New("local browser owner mismatch") } if reason := t.blocked.Load(); reason != nil { diff --git a/internal/agent/tools/browserskill_test.go b/internal/agent/tools/browserskill_test.go index 141b26cb..320a5e5d 100644 --- a/internal/agent/tools/browserskill_test.go +++ b/internal/agent/tools/browserskill_test.go @@ -27,6 +27,44 @@ func TestBrowserSkillNeedsConnectionNotSandbox(t *testing.T) { require.ErrorContains(t, err, "owner mismatch") } +type browserScopeManager struct { + browserLifecycleManager + scopes []browserskill.Scope +} + +func (m *browserScopeManager) Call( + ctx context.Context, s browserskill.Scope, session, method string, params map[string]any, +) (json.RawMessage, error) { + m.scopes = append(m.scopes, s) + return m.browserLifecycleManager.Call(ctx, s, session, method, params) +} + +// A shared agent executes in its owner's workspace; the browser must still be +// the one the asking member paired in their own workspace. +func TestBrowserSkillScopeFollowsCallerInSharedAgent(t *testing.T) { + ctx := context.WithValue(t.Context(), types.TenantIDContextKey, uint64(7)) + ctx = context.WithValue(ctx, types.UserIDContextKey, "alice") + shared := types.WithExecutionTenant(ctx, 99) + own := browserskill.Scope{Tenant: 7, User: "alice"} + require.Equal(t, own, BrowserSkillScope(ctx)) + require.Equal(t, own, BrowserSkillScope(shared)) + + manager := &browserScopeManager{} + tool := NewBrowserSkillTool(nil, BrowserSkillScope(shared), "chat") + tool.manager = manager + result, err := tool.Execute(shared, json.RawMessage(`{"method":"observe"}`)) + require.NoError(t, err) + require.True(t, result.Success) + require.Equal(t, []browserskill.Scope{own}, manager.scopes) + + bob := types.WithExecutionTenant(context.WithValue(ctx, types.UserIDContextKey, "bob"), 99) + _, err = tool.Execute(bob, json.RawMessage(`{"method":"observe"}`)) + require.ErrorContains(t, err, "owner mismatch") + _, err = tool.Execute(context.WithValue(ctx, types.TenantIDContextKey, uint64(8)), + json.RawMessage(`{"method":"observe"}`)) + require.ErrorContains(t, err, "owner mismatch", "another workspace's member must not drive this browser") +} + func TestBrowserWaitRequiresDocumentedDuration(t *testing.T) { ctx := context.WithValue(context.Background(), types.TenantIDContextKey, uint64(7)) ctx = context.WithValue(ctx, types.UserIDContextKey, "alice") diff --git a/internal/application/service/agent_service.go b/internal/application/service/agent_service.go index 8c2ae06e..becb6866 100644 --- a/internal/application/service/agent_service.go +++ b/internal/application/service/agent_service.go @@ -285,9 +285,7 @@ func (s *agentService) CreateAgentEngine( // Browser operations are native BrowserSkill RPCs, independent of shell and sandbox setup. if config.LocalBrowserEnabled && s.browserSkill.Enabled() && !config.SkillInstallMode() { - tenant, _ := types.TenantIDFromContext(ctx) - user, _ := types.UserIDFromContext(ctx) - scope := browserskill.Scope{Tenant: tenant, User: user} + scope := tools.BrowserSkillScope(ctx) instructions, err := s.browserSearchInstructions(ctx) if err != nil { return nil, err