2026-08-28 06:11:11 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
|
|
"github.com/Tencent/WeKnora/internal/application/service"
|
|
|
|
|
apperrors "github.com/Tencent/WeKnora/internal/errors"
|
|
|
|
|
"github.com/Tencent/WeKnora/internal/middleware"
|
|
|
|
|
"github.com/Tencent/WeKnora/internal/types"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type fakeSkillCatalog struct {
|
2026-09-02 04:22:48 +00:00
|
|
|
list []service.SkillCatalogView
|
|
|
|
|
registerID string
|
|
|
|
|
installs map[string]string
|
|
|
|
|
installErrs map[string]string
|
|
|
|
|
deleteErr error
|
|
|
|
|
source string
|
|
|
|
|
installCalls int
|
2026-08-28 06:11:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (f *fakeSkillCatalog) ListCatalog(context.Context, uint64) ([]service.SkillCatalogView, error) {
|
|
|
|
|
return f.list, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (f *fakeSkillCatalog) RegisterCatalogFromArchive(
|
|
|
|
|
context.Context, uint64, []byte,
|
|
|
|
|
) (*types.TenantSkillCatalogEntity, error) {
|
|
|
|
|
return &types.TenantSkillCatalogEntity{ID: f.registerID, Name: "pdf"}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (f *fakeSkillCatalog) RegisterCatalogFromSource(
|
|
|
|
|
_ context.Context, _ uint64, source string,
|
|
|
|
|
) (*types.TenantSkillCatalogEntity, error) {
|
|
|
|
|
f.source = source
|
|
|
|
|
return &types.TenantSkillCatalogEntity{ID: f.registerID, Name: "pdf"}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (f *fakeSkillCatalog) InstallCatalogToConfigs(
|
|
|
|
|
context.Context, uint64, string, []string,
|
2026-08-28 07:06:58 +00:00
|
|
|
) (*service.CatalogInstallResult, error) {
|
2026-09-02 04:22:48 +00:00
|
|
|
f.installCalls++
|
2026-08-28 07:06:58 +00:00
|
|
|
return &service.CatalogInstallResult{Installs: f.installs, Errors: f.installErrs}, nil
|
2026-08-28 06:11:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (f *fakeSkillCatalog) DeleteCatalog(context.Context, uint64, string) error {
|
|
|
|
|
return f.deleteErr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (f *fakeSkillCatalog) ListCatalogFiles(context.Context, uint64, string) ([]service.SkillFileEntry, error) {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (f *fakeSkillCatalog) ReadCatalogFile(context.Context, uint64, string, string) (*service.SkillFileContent, error) {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newCatalogRouter(h *SkillHandler) *gin.Engine {
|
|
|
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
|
r := gin.New()
|
|
|
|
|
r.Use(middleware.ErrorHandler())
|
|
|
|
|
r.Use(func(c *gin.Context) {
|
|
|
|
|
c.Set(types.TenantIDContextKey.String(), testSkillTenantID)
|
|
|
|
|
c.Next()
|
|
|
|
|
})
|
|
|
|
|
r.GET("/skills/catalog", h.ListCatalog)
|
|
|
|
|
r.POST("/skills/catalog", h.RegisterCatalog)
|
|
|
|
|
r.POST("/skills/catalog/:id/install", h.InstallCatalog)
|
|
|
|
|
r.DELETE("/skills/catalog/:id", h.DeleteCatalog)
|
|
|
|
|
return r
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestListCatalogReturnsDefinitionsAndInstallations(t *testing.T) {
|
|
|
|
|
now := time.Now()
|
|
|
|
|
catalog := &fakeSkillCatalog{
|
|
|
|
|
list: []service.SkillCatalogView{{
|
|
|
|
|
ID: "cat-1", Name: "pdf", Description: "extract", CreatedAt: now, UpdatedAt: now,
|
|
|
|
|
Installations: []service.SkillCatalogInstallView{{
|
|
|
|
|
SkillID: "sk-1", SandboxConfigID: "cfg-1", SandboxConfigName: "prod",
|
|
|
|
|
Status: types.SkillStatusReady, Enabled: true, UpdatedAt: now,
|
|
|
|
|
}},
|
|
|
|
|
}},
|
|
|
|
|
}
|
fix(agent): scope the @Skill and @MCP pickers to the agent that will run (#3475)
* fix(agent): scope the @Skill and @MCP pickers to the agent that will run
Both pickers listed the caller's own workspace, but a shared agent invokes its
OWNER's skills and MCP services. The two never line up, so for every shared
agent:
* @Skill was always empty, in every skills mode. GET /skills resolves the
sandbox config with (tenant, id), and the agent's config id does not exist
in the borrower's workspace - effectiveTenantSkills short-circuits before
it reads a single skill row. The response still reported
skills_available: true, so the category appeared with nothing under it.
* @MCP in "selected" mode was empty for the same reason: the preset holds the
owner's service ids, which match nothing in the borrower's catalog.
* @MCP in "all" mode was worse than empty. isMCPAllowedByAgent returns true
for every enabled service, so the picker offered the BORROWER's own
services; resolvePerRequestMCPScope then intersected the mention against
the agent's preset, found nothing, and dropped it with only a warning.
Resolve the agent when the request carries agent_id + agent_source_tenant_id
and read its resources where they live. Only an explicit source workspace takes
the share path, so an own-agent request is byte-for-byte unchanged; built-in
agent ids exist in every workspace, and falling back to a share lookup without
one would let an org member take over the caller's default agent, exactly as
session.Handler.resolveAgent already documents.
Both endpoints serve only what the agent can actually invoke:
* skills: the sandbox config comes from the agent, never from the query, so a
borrower cannot name another config in the owner's workspace and enumerate
it; "selected" narrows to the configured names and a disabled picker never
reaches the owner's inventory at all.
* MCP: the agent's explicit preset and nothing else, because that is exactly
what resolvePerRequestMCPScope accepts for a shared agent. "all" therefore
offers nothing rather than offering services every mention would drop.
Whether "all" should become mentionable is a separate decision about that
guard, not about the picker.
The cross-workspace MCP rows use a narrowed response built field by field.
NewMCPServiceResponse reveals endpoint URL, headers, env vars and stdio command
to an Admin, and a borrower can be an Admin of their own workspace - that shape
must be narrow by construction, not by role.
On the client the two lists now follow the selected agent: their cache keys
carry it, and the chat input refetches on a switch behind a request token, so a
slow response for the agent the user just left cannot overwrite the picker with
another workspace's services.
* fix(agent): check the c.Error return on the shared-agent picker paths
golangci-lint judges pull requests on the lines they introduce, so the
repository's older bare c.Error calls are grandfathered while these three are
not. Match the _ = c.Error form already used in mcp_service.go.
2026-09-21 05:02:34 +00:00
|
|
|
router := newCatalogRouter(NewSkillHandler(&fakeUsableSkillLister{}, catalog, nil))
|
2026-08-28 06:11:11 +00:00
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
router.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/skills/catalog", nil))
|
|
|
|
|
require.Equal(t, http.StatusOK, w.Code)
|
|
|
|
|
|
|
|
|
|
var body struct {
|
|
|
|
|
Success bool
|
|
|
|
|
Data []service.SkillCatalogView
|
|
|
|
|
}
|
|
|
|
|
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &body))
|
|
|
|
|
require.True(t, body.Success)
|
|
|
|
|
require.Len(t, body.Data, 1)
|
|
|
|
|
require.Equal(t, "pdf", body.Data[0].Name)
|
|
|
|
|
require.Equal(t, "cfg-1", body.Data[0].Installations[0].SandboxConfigID)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestRegisterCatalogFromSource(t *testing.T) {
|
|
|
|
|
catalog := &fakeSkillCatalog{registerID: "cat-9"}
|
fix(agent): scope the @Skill and @MCP pickers to the agent that will run (#3475)
* fix(agent): scope the @Skill and @MCP pickers to the agent that will run
Both pickers listed the caller's own workspace, but a shared agent invokes its
OWNER's skills and MCP services. The two never line up, so for every shared
agent:
* @Skill was always empty, in every skills mode. GET /skills resolves the
sandbox config with (tenant, id), and the agent's config id does not exist
in the borrower's workspace - effectiveTenantSkills short-circuits before
it reads a single skill row. The response still reported
skills_available: true, so the category appeared with nothing under it.
* @MCP in "selected" mode was empty for the same reason: the preset holds the
owner's service ids, which match nothing in the borrower's catalog.
* @MCP in "all" mode was worse than empty. isMCPAllowedByAgent returns true
for every enabled service, so the picker offered the BORROWER's own
services; resolvePerRequestMCPScope then intersected the mention against
the agent's preset, found nothing, and dropped it with only a warning.
Resolve the agent when the request carries agent_id + agent_source_tenant_id
and read its resources where they live. Only an explicit source workspace takes
the share path, so an own-agent request is byte-for-byte unchanged; built-in
agent ids exist in every workspace, and falling back to a share lookup without
one would let an org member take over the caller's default agent, exactly as
session.Handler.resolveAgent already documents.
Both endpoints serve only what the agent can actually invoke:
* skills: the sandbox config comes from the agent, never from the query, so a
borrower cannot name another config in the owner's workspace and enumerate
it; "selected" narrows to the configured names and a disabled picker never
reaches the owner's inventory at all.
* MCP: the agent's explicit preset and nothing else, because that is exactly
what resolvePerRequestMCPScope accepts for a shared agent. "all" therefore
offers nothing rather than offering services every mention would drop.
Whether "all" should become mentionable is a separate decision about that
guard, not about the picker.
The cross-workspace MCP rows use a narrowed response built field by field.
NewMCPServiceResponse reveals endpoint URL, headers, env vars and stdio command
to an Admin, and a borrower can be an Admin of their own workspace - that shape
must be narrow by construction, not by role.
On the client the two lists now follow the selected agent: their cache keys
carry it, and the chat input refetches on a switch behind a request token, so a
slow response for the agent the user just left cannot overwrite the picker with
another workspace's services.
* fix(agent): check the c.Error return on the shared-agent picker paths
golangci-lint judges pull requests on the lines they introduce, so the
repository's older bare c.Error calls are grandfathered while these three are
not. Match the _ = c.Error form already used in mcp_service.go.
2026-09-21 05:02:34 +00:00
|
|
|
router := newCatalogRouter(NewSkillHandler(&fakeUsableSkillLister{}, catalog, nil))
|
2026-08-28 06:11:11 +00:00
|
|
|
|
|
|
|
|
body, err := json.Marshal(map[string]string{"source": "@owner/pdf"})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/skills/catalog", bytes.NewReader(body))
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
router.ServeHTTP(w, req)
|
|
|
|
|
require.Equal(t, http.StatusCreated, w.Code)
|
|
|
|
|
require.Equal(t, "@owner/pdf", catalog.source)
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-02 04:07:06 +00:00
|
|
|
func TestRegisterCatalogFromSourceRejectsAnOversizedJSONBody(t *testing.T) {
|
|
|
|
|
catalog := &fakeSkillCatalog{registerID: "cat-9"}
|
fix(agent): scope the @Skill and @MCP pickers to the agent that will run (#3475)
* fix(agent): scope the @Skill and @MCP pickers to the agent that will run
Both pickers listed the caller's own workspace, but a shared agent invokes its
OWNER's skills and MCP services. The two never line up, so for every shared
agent:
* @Skill was always empty, in every skills mode. GET /skills resolves the
sandbox config with (tenant, id), and the agent's config id does not exist
in the borrower's workspace - effectiveTenantSkills short-circuits before
it reads a single skill row. The response still reported
skills_available: true, so the category appeared with nothing under it.
* @MCP in "selected" mode was empty for the same reason: the preset holds the
owner's service ids, which match nothing in the borrower's catalog.
* @MCP in "all" mode was worse than empty. isMCPAllowedByAgent returns true
for every enabled service, so the picker offered the BORROWER's own
services; resolvePerRequestMCPScope then intersected the mention against
the agent's preset, found nothing, and dropped it with only a warning.
Resolve the agent when the request carries agent_id + agent_source_tenant_id
and read its resources where they live. Only an explicit source workspace takes
the share path, so an own-agent request is byte-for-byte unchanged; built-in
agent ids exist in every workspace, and falling back to a share lookup without
one would let an org member take over the caller's default agent, exactly as
session.Handler.resolveAgent already documents.
Both endpoints serve only what the agent can actually invoke:
* skills: the sandbox config comes from the agent, never from the query, so a
borrower cannot name another config in the owner's workspace and enumerate
it; "selected" narrows to the configured names and a disabled picker never
reaches the owner's inventory at all.
* MCP: the agent's explicit preset and nothing else, because that is exactly
what resolvePerRequestMCPScope accepts for a shared agent. "all" therefore
offers nothing rather than offering services every mention would drop.
Whether "all" should become mentionable is a separate decision about that
guard, not about the picker.
The cross-workspace MCP rows use a narrowed response built field by field.
NewMCPServiceResponse reveals endpoint URL, headers, env vars and stdio command
to an Admin, and a borrower can be an Admin of their own workspace - that shape
must be narrow by construction, not by role.
On the client the two lists now follow the selected agent: their cache keys
carry it, and the chat input refetches on a switch behind a request token, so a
slow response for the agent the user just left cannot overwrite the picker with
another workspace's services.
* fix(agent): check the c.Error return on the shared-agent picker paths
golangci-lint judges pull requests on the lines they introduce, so the
repository's older bare c.Error calls are grandfathered while these three are
not. Match the _ = c.Error form already used in mcp_service.go.
2026-09-21 05:02:34 +00:00
|
|
|
router := newCatalogRouter(NewSkillHandler(&fakeUsableSkillLister{}, catalog, nil))
|
2026-09-02 04:07:06 +00:00
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/skills/catalog",
|
|
|
|
|
bytes.NewReader(oversizedSkillSourceJSON(1)))
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
router.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
|
|
require.Equal(t, http.StatusBadRequest, w.Code)
|
|
|
|
|
require.Contains(t, w.Body.String(), "skill source request is too large")
|
|
|
|
|
require.Empty(t, catalog.source)
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-02 04:22:48 +00:00
|
|
|
func TestInstallCatalogRejectsAnOversizedJSONBody(t *testing.T) {
|
|
|
|
|
catalog := &fakeSkillCatalog{installs: map[string]string{"cfg-1": "sk-1"}}
|
fix(agent): scope the @Skill and @MCP pickers to the agent that will run (#3475)
* fix(agent): scope the @Skill and @MCP pickers to the agent that will run
Both pickers listed the caller's own workspace, but a shared agent invokes its
OWNER's skills and MCP services. The two never line up, so for every shared
agent:
* @Skill was always empty, in every skills mode. GET /skills resolves the
sandbox config with (tenant, id), and the agent's config id does not exist
in the borrower's workspace - effectiveTenantSkills short-circuits before
it reads a single skill row. The response still reported
skills_available: true, so the category appeared with nothing under it.
* @MCP in "selected" mode was empty for the same reason: the preset holds the
owner's service ids, which match nothing in the borrower's catalog.
* @MCP in "all" mode was worse than empty. isMCPAllowedByAgent returns true
for every enabled service, so the picker offered the BORROWER's own
services; resolvePerRequestMCPScope then intersected the mention against
the agent's preset, found nothing, and dropped it with only a warning.
Resolve the agent when the request carries agent_id + agent_source_tenant_id
and read its resources where they live. Only an explicit source workspace takes
the share path, so an own-agent request is byte-for-byte unchanged; built-in
agent ids exist in every workspace, and falling back to a share lookup without
one would let an org member take over the caller's default agent, exactly as
session.Handler.resolveAgent already documents.
Both endpoints serve only what the agent can actually invoke:
* skills: the sandbox config comes from the agent, never from the query, so a
borrower cannot name another config in the owner's workspace and enumerate
it; "selected" narrows to the configured names and a disabled picker never
reaches the owner's inventory at all.
* MCP: the agent's explicit preset and nothing else, because that is exactly
what resolvePerRequestMCPScope accepts for a shared agent. "all" therefore
offers nothing rather than offering services every mention would drop.
Whether "all" should become mentionable is a separate decision about that
guard, not about the picker.
The cross-workspace MCP rows use a narrowed response built field by field.
NewMCPServiceResponse reveals endpoint URL, headers, env vars and stdio command
to an Admin, and a borrower can be an Admin of their own workspace - that shape
must be narrow by construction, not by role.
On the client the two lists now follow the selected agent: their cache keys
carry it, and the chat input refetches on a switch behind a request token, so a
slow response for the agent the user just left cannot overwrite the picker with
another workspace's services.
* fix(agent): check the c.Error return on the shared-agent picker paths
golangci-lint judges pull requests on the lines they introduce, so the
repository's older bare c.Error calls are grandfathered while these three are
not. Match the _ = c.Error form already used in mcp_service.go.
2026-09-21 05:02:34 +00:00
|
|
|
router := newCatalogRouter(NewSkillHandler(&fakeUsableSkillLister{}, catalog, nil))
|
2026-09-02 04:22:48 +00:00
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/skills/catalog/cat-1/install",
|
|
|
|
|
bytes.NewReader(oversizedSkillSourceJSON(1)))
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
router.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
|
|
require.Equal(t, http.StatusBadRequest, w.Code)
|
|
|
|
|
require.Contains(t, w.Body.String(), "skill request is too large")
|
|
|
|
|
require.Equal(t, 0, catalog.installCalls)
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-28 06:11:11 +00:00
|
|
|
func TestInstallCatalogAcceptsPerConfigIDs(t *testing.T) {
|
|
|
|
|
catalog := &fakeSkillCatalog{installs: map[string]string{"cfg-1": "sk-1"}}
|
fix(agent): scope the @Skill and @MCP pickers to the agent that will run (#3475)
* fix(agent): scope the @Skill and @MCP pickers to the agent that will run
Both pickers listed the caller's own workspace, but a shared agent invokes its
OWNER's skills and MCP services. The two never line up, so for every shared
agent:
* @Skill was always empty, in every skills mode. GET /skills resolves the
sandbox config with (tenant, id), and the agent's config id does not exist
in the borrower's workspace - effectiveTenantSkills short-circuits before
it reads a single skill row. The response still reported
skills_available: true, so the category appeared with nothing under it.
* @MCP in "selected" mode was empty for the same reason: the preset holds the
owner's service ids, which match nothing in the borrower's catalog.
* @MCP in "all" mode was worse than empty. isMCPAllowedByAgent returns true
for every enabled service, so the picker offered the BORROWER's own
services; resolvePerRequestMCPScope then intersected the mention against
the agent's preset, found nothing, and dropped it with only a warning.
Resolve the agent when the request carries agent_id + agent_source_tenant_id
and read its resources where they live. Only an explicit source workspace takes
the share path, so an own-agent request is byte-for-byte unchanged; built-in
agent ids exist in every workspace, and falling back to a share lookup without
one would let an org member take over the caller's default agent, exactly as
session.Handler.resolveAgent already documents.
Both endpoints serve only what the agent can actually invoke:
* skills: the sandbox config comes from the agent, never from the query, so a
borrower cannot name another config in the owner's workspace and enumerate
it; "selected" narrows to the configured names and a disabled picker never
reaches the owner's inventory at all.
* MCP: the agent's explicit preset and nothing else, because that is exactly
what resolvePerRequestMCPScope accepts for a shared agent. "all" therefore
offers nothing rather than offering services every mention would drop.
Whether "all" should become mentionable is a separate decision about that
guard, not about the picker.
The cross-workspace MCP rows use a narrowed response built field by field.
NewMCPServiceResponse reveals endpoint URL, headers, env vars and stdio command
to an Admin, and a borrower can be an Admin of their own workspace - that shape
must be narrow by construction, not by role.
On the client the two lists now follow the selected agent: their cache keys
carry it, and the chat input refetches on a switch behind a request token, so a
slow response for the agent the user just left cannot overwrite the picker with
another workspace's services.
* fix(agent): check the c.Error return on the shared-agent picker paths
golangci-lint judges pull requests on the lines they introduce, so the
repository's older bare c.Error calls are grandfathered while these three are
not. Match the _ = c.Error form already used in mcp_service.go.
2026-09-21 05:02:34 +00:00
|
|
|
router := newCatalogRouter(NewSkillHandler(&fakeUsableSkillLister{}, catalog, nil))
|
2026-08-28 06:11:11 +00:00
|
|
|
|
|
|
|
|
body, err := json.Marshal(map[string][]string{"sandbox_config_ids": {"cfg-1"}})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/skills/catalog/cat-1/install", bytes.NewReader(body))
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
router.ServeHTTP(w, req)
|
|
|
|
|
require.Equal(t, http.StatusAccepted, w.Code)
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-28 07:06:58 +00:00
|
|
|
func TestInstallCatalogIncludesPerConfigErrors(t *testing.T) {
|
|
|
|
|
catalog := &fakeSkillCatalog{
|
|
|
|
|
installs: map[string]string{"cfg-1": "sk-1"},
|
|
|
|
|
installErrs: map[string]string{"cfg-2": "sandbox config not found"},
|
|
|
|
|
}
|
fix(agent): scope the @Skill and @MCP pickers to the agent that will run (#3475)
* fix(agent): scope the @Skill and @MCP pickers to the agent that will run
Both pickers listed the caller's own workspace, but a shared agent invokes its
OWNER's skills and MCP services. The two never line up, so for every shared
agent:
* @Skill was always empty, in every skills mode. GET /skills resolves the
sandbox config with (tenant, id), and the agent's config id does not exist
in the borrower's workspace - effectiveTenantSkills short-circuits before
it reads a single skill row. The response still reported
skills_available: true, so the category appeared with nothing under it.
* @MCP in "selected" mode was empty for the same reason: the preset holds the
owner's service ids, which match nothing in the borrower's catalog.
* @MCP in "all" mode was worse than empty. isMCPAllowedByAgent returns true
for every enabled service, so the picker offered the BORROWER's own
services; resolvePerRequestMCPScope then intersected the mention against
the agent's preset, found nothing, and dropped it with only a warning.
Resolve the agent when the request carries agent_id + agent_source_tenant_id
and read its resources where they live. Only an explicit source workspace takes
the share path, so an own-agent request is byte-for-byte unchanged; built-in
agent ids exist in every workspace, and falling back to a share lookup without
one would let an org member take over the caller's default agent, exactly as
session.Handler.resolveAgent already documents.
Both endpoints serve only what the agent can actually invoke:
* skills: the sandbox config comes from the agent, never from the query, so a
borrower cannot name another config in the owner's workspace and enumerate
it; "selected" narrows to the configured names and a disabled picker never
reaches the owner's inventory at all.
* MCP: the agent's explicit preset and nothing else, because that is exactly
what resolvePerRequestMCPScope accepts for a shared agent. "all" therefore
offers nothing rather than offering services every mention would drop.
Whether "all" should become mentionable is a separate decision about that
guard, not about the picker.
The cross-workspace MCP rows use a narrowed response built field by field.
NewMCPServiceResponse reveals endpoint URL, headers, env vars and stdio command
to an Admin, and a borrower can be an Admin of their own workspace - that shape
must be narrow by construction, not by role.
On the client the two lists now follow the selected agent: their cache keys
carry it, and the chat input refetches on a switch behind a request token, so a
slow response for the agent the user just left cannot overwrite the picker with
another workspace's services.
* fix(agent): check the c.Error return on the shared-agent picker paths
golangci-lint judges pull requests on the lines they introduce, so the
repository's older bare c.Error calls are grandfathered while these three are
not. Match the _ = c.Error form already used in mcp_service.go.
2026-09-21 05:02:34 +00:00
|
|
|
router := newCatalogRouter(NewSkillHandler(&fakeUsableSkillLister{}, catalog, nil))
|
2026-08-28 07:06:58 +00:00
|
|
|
|
|
|
|
|
body, err := json.Marshal(map[string][]string{"sandbox_config_ids": {"cfg-1", "cfg-2"}})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/skills/catalog/cat-1/install", bytes.NewReader(body))
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
router.ServeHTTP(w, req)
|
|
|
|
|
require.Equal(t, http.StatusAccepted, w.Code)
|
|
|
|
|
|
|
|
|
|
var payload struct {
|
|
|
|
|
Success bool `json:"success"`
|
|
|
|
|
Data struct {
|
|
|
|
|
Installs map[string]string `json:"installs"`
|
|
|
|
|
Errors map[string]string `json:"errors"`
|
|
|
|
|
} `json:"data"`
|
|
|
|
|
}
|
|
|
|
|
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &payload))
|
|
|
|
|
require.False(t, payload.Success)
|
|
|
|
|
require.Equal(t, "sk-1", payload.Data.Installs["cfg-1"])
|
|
|
|
|
require.Equal(t, "sandbox config not found", payload.Data.Errors["cfg-2"])
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-28 06:11:11 +00:00
|
|
|
func TestDeleteCatalogRefusesWhileInstalled(t *testing.T) {
|
|
|
|
|
catalog := &fakeSkillCatalog{
|
|
|
|
|
deleteErr: apperrors.NewConflictError("remove this skill from every sandbox"),
|
|
|
|
|
}
|
fix(agent): scope the @Skill and @MCP pickers to the agent that will run (#3475)
* fix(agent): scope the @Skill and @MCP pickers to the agent that will run
Both pickers listed the caller's own workspace, but a shared agent invokes its
OWNER's skills and MCP services. The two never line up, so for every shared
agent:
* @Skill was always empty, in every skills mode. GET /skills resolves the
sandbox config with (tenant, id), and the agent's config id does not exist
in the borrower's workspace - effectiveTenantSkills short-circuits before
it reads a single skill row. The response still reported
skills_available: true, so the category appeared with nothing under it.
* @MCP in "selected" mode was empty for the same reason: the preset holds the
owner's service ids, which match nothing in the borrower's catalog.
* @MCP in "all" mode was worse than empty. isMCPAllowedByAgent returns true
for every enabled service, so the picker offered the BORROWER's own
services; resolvePerRequestMCPScope then intersected the mention against
the agent's preset, found nothing, and dropped it with only a warning.
Resolve the agent when the request carries agent_id + agent_source_tenant_id
and read its resources where they live. Only an explicit source workspace takes
the share path, so an own-agent request is byte-for-byte unchanged; built-in
agent ids exist in every workspace, and falling back to a share lookup without
one would let an org member take over the caller's default agent, exactly as
session.Handler.resolveAgent already documents.
Both endpoints serve only what the agent can actually invoke:
* skills: the sandbox config comes from the agent, never from the query, so a
borrower cannot name another config in the owner's workspace and enumerate
it; "selected" narrows to the configured names and a disabled picker never
reaches the owner's inventory at all.
* MCP: the agent's explicit preset and nothing else, because that is exactly
what resolvePerRequestMCPScope accepts for a shared agent. "all" therefore
offers nothing rather than offering services every mention would drop.
Whether "all" should become mentionable is a separate decision about that
guard, not about the picker.
The cross-workspace MCP rows use a narrowed response built field by field.
NewMCPServiceResponse reveals endpoint URL, headers, env vars and stdio command
to an Admin, and a borrower can be an Admin of their own workspace - that shape
must be narrow by construction, not by role.
On the client the two lists now follow the selected agent: their cache keys
carry it, and the chat input refetches on a switch behind a request token, so a
slow response for the agent the user just left cannot overwrite the picker with
another workspace's services.
* fix(agent): check the c.Error return on the shared-agent picker paths
golangci-lint judges pull requests on the lines they introduce, so the
repository's older bare c.Error calls are grandfathered while these three are
not. Match the _ = c.Error form already used in mcp_service.go.
2026-09-21 05:02:34 +00:00
|
|
|
router := newCatalogRouter(NewSkillHandler(&fakeUsableSkillLister{}, catalog, nil))
|
2026-08-28 06:11:11 +00:00
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
router.ServeHTTP(w, httptest.NewRequest(http.MethodDelete, "/skills/catalog/cat-1", nil))
|
|
|
|
|
require.Equal(t, http.StatusConflict, w.Code)
|
|
|
|
|
}
|