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.
main
wizardchen 2026-09-24 17:13:48 +08:00 committed by lyingbug
parent eebc1d8643
commit e6284f4eac
3 changed files with 47 additions and 6 deletions

View File

@ -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 {

View File

@ -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")

View File

@ -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