347 lines
12 KiB
Go
347 lines
12 KiB
Go
package archiegateway
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"maps"
|
|
"path/filepath"
|
|
"slices"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/modelcontextprotocol/go-sdk/mcp"
|
|
"github.com/samcharles93/ai-sdk/runtime"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
|
|
"github.com/samcharles93/archie-core/internal/agentexec"
|
|
"github.com/samcharles93/archie-core/internal/agentexec/modelloop"
|
|
"github.com/samcharles93/archie-core/internal/config"
|
|
pb "github.com/samcharles93/archie-core/internal/contracts/gateway/v1"
|
|
"github.com/samcharles93/archie-core/internal/domain/access"
|
|
"github.com/samcharles93/archie-core/internal/domain/identity"
|
|
"github.com/samcharles93/archie-core/internal/domain/org"
|
|
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
|
"github.com/samcharles93/archie-core/internal/domain/storepkg"
|
|
"github.com/samcharles93/archie-core/internal/domain/usage"
|
|
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
|
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/taskmcp"
|
|
"github.com/samcharles93/archie-core/internal/tools"
|
|
protocol "github.com/samcharles93/archie-core/internal/tools/mcp"
|
|
)
|
|
|
|
type taskToolSession struct {
|
|
mu sync.Mutex
|
|
lifetime context.Context //nolint:containedctx // Providers belong to the run, not one RPC.
|
|
providers *taskmcp.Set
|
|
configHash [32]byte
|
|
cancel context.CancelFunc
|
|
}
|
|
|
|
type taskToolAuthority interface {
|
|
TaskPrincipalForCredential(context.Context, string) (*task.Task, identity.IdentityID, error)
|
|
PrincipalFor(context.Context, identity.IdentityID) (access.Principal, error)
|
|
GetInstalled(context.Context, string) (storepkg.Installed, error)
|
|
TaskModels(context.Context, int64) (config.OrgModels, error)
|
|
RecordUsage(context.Context, usage.Record) error
|
|
}
|
|
|
|
type taskToolsServer struct {
|
|
pb.UnimplementedTaskToolsServiceServer
|
|
gateway *server
|
|
authority taskToolAuthority
|
|
configFor func(context.Context) (config.Config, error)
|
|
mu sync.Mutex
|
|
sessions map[[32]byte]*taskToolSession
|
|
}
|
|
|
|
func (s *taskToolsServer) authorize(ctx context.Context, credential string) (context.Context, *task.Task, config.Config, []string, error) {
|
|
if access.SourceFromContext(ctx) != "archied" {
|
|
return nil, nil, config.Config{}, nil, status.Error(codes.PermissionDenied, "task tools require the daemon")
|
|
}
|
|
if credential == "" {
|
|
return nil, nil, config.Config{}, nil, status.Error(codes.Unauthenticated, "task grant required")
|
|
}
|
|
t, principal, err := s.authority.TaskPrincipalForCredential(ctx, credential)
|
|
if err != nil {
|
|
s.close(ctx, credential)
|
|
code := codes.Unauthenticated
|
|
if errors.Is(err, storecontract.ErrUnavailable) {
|
|
code = codes.Unavailable
|
|
}
|
|
return nil, nil, config.Config{}, nil, status.Error(code, "task grant unavailable")
|
|
}
|
|
if t.Status != "running" {
|
|
s.close(ctx, credential)
|
|
return nil, nil, config.Config{}, nil, status.Error(codes.PermissionDenied, "task is not running")
|
|
}
|
|
p, err := s.authority.PrincipalFor(ctx, principal)
|
|
if err != nil || string(p.Org) != t.OrgOrDefault() {
|
|
return nil, nil, config.Config{}, nil, status.Error(codes.PermissionDenied, "task identity unavailable")
|
|
}
|
|
ctx = org.WithScope(org.WithOrg(access.WithPrincipal(access.WithActor(ctx, string(principal)), p), p.Org), p.Org)
|
|
iface, err := task.ParseWorkflowInterface(t.WorkflowDefinitionYAML)
|
|
if err != nil {
|
|
return nil, nil, config.Config{}, nil, status.Error(codes.FailedPrecondition, "task workflow unavailable")
|
|
}
|
|
cfg, err := s.configFor(ctx)
|
|
if err != nil {
|
|
return nil, nil, config.Config{}, nil, status.Error(codes.Unavailable, "task tool settings unavailable")
|
|
}
|
|
profile, err := cfg.Containers.Profile(iface.Profile)
|
|
if err != nil || profile.Kit == "" {
|
|
return nil, nil, config.Config{}, nil, status.Error(codes.FailedPrecondition, "task has no Kit profile")
|
|
}
|
|
packaged := workflow.PackageOf(t.WorkflowDefinitionYAML)
|
|
var accepted []string
|
|
if packaged != "" {
|
|
installed, err := s.authority.GetInstalled(ctx, packaged)
|
|
if err == nil && installed.AcceptedAuthority != nil {
|
|
accepted = installed.AcceptedAuthority.Tools
|
|
}
|
|
}
|
|
return ctx, t, cfg, storepkg.AllowedTools(profile.Tools, packaged != "", accepted), nil
|
|
}
|
|
|
|
func taskToolAllowed(entry tools.ToolEntry, allowed []string) bool {
|
|
return entry.Toolset != "workspace" && entry.Name != "send_file" && !entry.Classification.IsApprovalRequired() && (allowed == nil || slices.Contains(allowed, entry.Name))
|
|
}
|
|
|
|
func (s *taskToolsServer) entries(ctx context.Context, t *task.Task, cfg config.Config, credential string, allowed []string) ([]tools.ToolEntry, error) {
|
|
entries := []tools.ToolEntry{}
|
|
if allowed != nil && len(allowed) == 0 {
|
|
s.close(ctx, credential)
|
|
return entries, nil
|
|
}
|
|
for _, entry := range s.gateway.toolReg.All() {
|
|
// Task MCP providers have their own sampling identity and lifecycle.
|
|
if entry.Toolset != "mcp" && taskToolAllowed(entry, allowed) {
|
|
entries = append(entries, entry)
|
|
}
|
|
}
|
|
providers, err := s.providers(ctx, t, cfg, credential)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if providers != nil {
|
|
for _, entry := range providers.Registry.All() {
|
|
if taskToolAllowed(entry, allowed) {
|
|
entries = append(entries, entry)
|
|
}
|
|
}
|
|
}
|
|
return entries, nil
|
|
}
|
|
|
|
func (s *taskToolsServer) providers(ctx context.Context, t *task.Task, cfg config.Config, credential string) (*taskmcp.Set, error) {
|
|
if len(cfg.Tools.MCPServers) == 0 {
|
|
s.close(ctx, credential)
|
|
return nil, nil
|
|
}
|
|
models, err := s.authority.TaskModels(ctx, t.ID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("task sampling model: %w", err)
|
|
}
|
|
own := cfg.Providers
|
|
cfg.Providers = maps.Clone(s.gateway.cfgHolder.Get().Providers)
|
|
cfg = cfg.WithOrgModels(models)
|
|
keys := map[string]string{}
|
|
for name := range models.Providers {
|
|
provider, ok := own[name]
|
|
if !ok {
|
|
return nil, fmt.Errorf("task provider settings unavailable")
|
|
}
|
|
if provider.APIKey != (config.SecretRef{}) {
|
|
value, err := s.gateway.secrets.Resolve(provider.APIKey)
|
|
if err != nil || value == "" {
|
|
return nil, fmt.Errorf("task provider credential unavailable")
|
|
}
|
|
keys[name] = value
|
|
}
|
|
}
|
|
encoded, err := json.Marshal(struct {
|
|
Servers []config.MCPServer
|
|
Models map[string]string
|
|
Providers map[string]config.Provider
|
|
Keys map[string]string
|
|
Limits map[string]config.ModelLimits
|
|
}{cfg.Tools.MCPServers, cfg.Models, cfg.Providers, keys, cfg.ModelLimits})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return s.loadProviders(ctx, t, cfg, credential, keys, sha256.Sum256(encoded))
|
|
}
|
|
|
|
func (s *taskToolsServer) loadProviders(ctx context.Context, t *task.Task, cfg config.Config, credential string, keys map[string]string, hash [32]byte) (*taskmcp.Set, error) {
|
|
key := sha256.Sum256([]byte(credential))
|
|
s.mu.Lock()
|
|
current := s.sessions[key]
|
|
if current == nil || current.lifetime.Err() != nil {
|
|
lifetime, cancel := context.WithCancel(context.WithoutCancel(ctx))
|
|
current = &taskToolSession{lifetime: lifetime, cancel: cancel}
|
|
s.sessions[key] = current
|
|
}
|
|
s.mu.Unlock()
|
|
current.mu.Lock()
|
|
defer current.mu.Unlock()
|
|
if err := current.lifetime.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
if current.providers != nil {
|
|
if current.configHash == hash {
|
|
return current.providers, nil
|
|
}
|
|
current.providers.Close(context.WithoutCancel(ctx), s.gateway.log)
|
|
current.providers = nil
|
|
}
|
|
// Cancel an unfinished startup with its RPC; a ready provider stays run-scoped.
|
|
stop := context.AfterFunc(ctx, current.cancel)
|
|
defer stop()
|
|
llm := modelloop.NewRuntimeWithKeys(agentexec.ProvidersFromConfig(cfg.Providers), cfg.ModelLimits, keys)
|
|
cacheDir := ""
|
|
if cfg.WorkDir != "" {
|
|
cacheDir = filepath.Join(cfg.WorkDir, "mcp-npm-cache")
|
|
}
|
|
providers, err := taskmcp.Start(current.lifetime, cfg.Tools.MCPServers, s.taskSampling(llm, cfg, t, credential), s.gateway.log, cacheDir) //nolint:contextcheck // The run owns provider process lifetime.
|
|
if err != nil || ctx.Err() != nil {
|
|
current.cancel()
|
|
if providers != nil {
|
|
providers.Close(context.WithoutCancel(ctx), s.gateway.log)
|
|
}
|
|
return nil, errors.Join(err, ctx.Err())
|
|
}
|
|
current.providers, current.configHash = providers, hash
|
|
return providers, nil
|
|
}
|
|
|
|
func (s *taskToolsServer) ListTaskTools(ctx context.Context, req *pb.ListTaskToolsRequest) (*pb.ListTaskToolsResponse, error) {
|
|
ctx, t, cfg, allowed, err := s.authorize(ctx, req.RunCredential)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
entries, err := s.entries(ctx, t, cfg, req.RunCredential, allowed)
|
|
if err != nil {
|
|
return nil, status.Error(codes.Unavailable, "task tool providers unavailable")
|
|
}
|
|
set, err := modelloop.BuildToolSetFrom(entries, modelloop.ToolSetOptions{})
|
|
if err != nil {
|
|
return nil, status.Error(codes.Unavailable, "task tool discovery failed")
|
|
}
|
|
result := []*mcp.Tool{}
|
|
for _, entry := range entries {
|
|
tool, ok := set[entry.Name]
|
|
if !ok {
|
|
continue
|
|
}
|
|
result = append(result, &mcp.Tool{Name: tool.Name, Description: tool.Description, InputSchema: tool.Parameters})
|
|
}
|
|
data, err := json.Marshal(result)
|
|
if err != nil {
|
|
return nil, status.Error(codes.Internal, "encode task tools")
|
|
}
|
|
return &pb.ListTaskToolsResponse{ToolsJson: data}, nil
|
|
}
|
|
|
|
func (s *taskToolsServer) CallTaskTool(ctx context.Context, req *pb.CallTaskToolRequest) (*pb.CallTaskToolResponse, error) {
|
|
ctx, t, cfg, allowed, err := s.authorize(ctx, req.RunCredential)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
entries, err := s.entries(ctx, t, cfg, req.RunCredential, allowed)
|
|
if err != nil {
|
|
return nil, status.Error(codes.Unavailable, "task tool providers unavailable")
|
|
}
|
|
entry, ok := findTaskTool(entries, req.Name)
|
|
if !ok {
|
|
return nil, status.Error(codes.PermissionDenied, "tool is not allowed for this task")
|
|
}
|
|
result, err := executeTaskTool(ctx, entry, req.ArgumentsJson, cfg.Tools.Policy.MaxResultChars)
|
|
if err != nil {
|
|
return nil, status.Error(codes.InvalidArgument, "task tool call failed: "+err.Error())
|
|
}
|
|
data, err := json.Marshal(result)
|
|
if err != nil {
|
|
return nil, status.Error(codes.Internal, "encode task tool result")
|
|
}
|
|
return &pb.CallTaskToolResponse{ResultJson: data}, nil
|
|
}
|
|
|
|
func findTaskTool(entries []tools.ToolEntry, name string) (tools.ToolEntry, bool) {
|
|
for _, entry := range entries {
|
|
if entry.Name == name {
|
|
return entry, true
|
|
}
|
|
}
|
|
return tools.ToolEntry{}, false
|
|
}
|
|
|
|
func (s *taskToolsServer) CloseTaskTools(ctx context.Context, req *pb.CloseTaskToolsRequest) (*pb.CloseTaskToolsResponse, error) {
|
|
if access.SourceFromContext(ctx) != "archied" {
|
|
return nil, status.Error(codes.PermissionDenied, "task tools require the daemon")
|
|
}
|
|
s.close(ctx, req.RunCredential)
|
|
return &pb.CloseTaskToolsResponse{}, nil
|
|
}
|
|
|
|
func (s *taskToolsServer) close(parent context.Context, credential string) {
|
|
key := sha256.Sum256([]byte(credential))
|
|
s.mu.Lock()
|
|
current := s.sessions[key]
|
|
delete(s.sessions, key)
|
|
s.mu.Unlock()
|
|
if current == nil {
|
|
return
|
|
}
|
|
current.cancel()
|
|
ctx, cancel := context.WithTimeout(context.WithoutCancel(parent), 10*time.Second)
|
|
defer cancel()
|
|
current.mu.Lock()
|
|
defer current.mu.Unlock()
|
|
if current.providers != nil {
|
|
current.providers.Close(ctx, s.gateway.log)
|
|
}
|
|
}
|
|
|
|
func (s *taskToolsServer) Close(parent context.Context) {
|
|
s.mu.Lock()
|
|
sessions := s.sessions
|
|
s.sessions = make(map[[32]byte]*taskToolSession)
|
|
s.mu.Unlock()
|
|
for _, session := range sessions {
|
|
session.cancel()
|
|
ctx, cancel := context.WithTimeout(context.WithoutCancel(parent), 10*time.Second)
|
|
session.mu.Lock()
|
|
if session.providers != nil {
|
|
session.providers.Close(ctx, s.gateway.log)
|
|
}
|
|
session.mu.Unlock()
|
|
cancel()
|
|
}
|
|
}
|
|
|
|
func (b *server) taskToolsConfig(ctx context.Context) (config.Config, error) {
|
|
base := b.cfgHolder.Get()
|
|
// These documents belong to the task's org; absence must not inherit
|
|
// the instance's MCP credentials or agent profiles.
|
|
base.Tools.MCPServers = nil
|
|
base.Containers.Profiles = nil
|
|
base.Providers = nil
|
|
cfg, _, err := b.controlPlane.RuntimeConfig(ctx, base)
|
|
return cfg, err
|
|
}
|
|
|
|
func (s *taskToolsServer) taskSampling(llm *runtime.Runtime, cfg config.Config, t *task.Task, credential string) protocol.SamplingHandler {
|
|
sample := taskmcp.SamplingHandler(llm, cfg.ForTask(), taskmcp.UsageRecorder(s.authority, t, s.gateway.log))
|
|
return func(ctx context.Context, request protocol.SamplingRequest) (protocol.SamplingResult, error) {
|
|
live, _, err := s.authority.TaskPrincipalForCredential(ctx, credential)
|
|
if err != nil || live.ID != t.ID || live.Status != task.StatusRunning || live.OrgOrDefault() != t.OrgOrDefault() {
|
|
return protocol.SamplingResult{}, errors.New("task grant no longer authorizes sampling")
|
|
}
|
|
return sample(ctx, request)
|
|
}
|
|
}
|